{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 33,
   "id": "4ad195ce",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np # 数据处理最重要的模块\n",
    "import pandas as pd # 数据处理最重要的模块\n",
    "\n",
    "from IPython.core.interactiveshell import InteractiveShell # jupyter运行输出的模块\n",
    "\n",
    "#显示每一个运行结果\n",
    "InteractiveShell.ast_node_interactivity = 'all'\n",
    "\n",
    "#设置行不限制数量\n",
    "#pd.set_option('display.max_rows',None)\n",
    "\n",
    "#设置列不限制数量\n",
    "pd.set_option('display.max_columns', None)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "id": "316bdd7e",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 常用的 Jupyter/IPython 魔法命令（速查）\n",
    "# 魔法命令分为两类：行魔法（以 % 开头，作用于单行）与单元魔法（以 %% 开头，作用于整个单元）。使用 %lsmagic 可列出当前环境可用的所有魔法，%quickref 查看速查帮助，任意魔法后加 ? 可看用法说明。\n",
    "# 计时与性能分析\n",
    "# %time / %%time：测量单行或整个单元格的运行耗时，快速获得一次性计时结果。\n",
    "# %timeit / %%timeit：自动多次运行并给出统计（如均值、标准差），适合更可靠的微基准测试。\n",
    "# %prun：基于 cProfile 的函数级性能分析，查看不同函数的耗时构成。\n",
    "# 可选扩展：%lprun（逐行耗时，需要 line_profiler 扩展）与 %memit（内存峰值，需要 memory_profiler 扩展），需先 %load_ext line_profiler 或 %load_ext memory_profiler 才能使用。\n",
    "# 调试与异常\n",
    "# %debug：在异常发生后进入交互式调试器（pdb）。\n",
    "# %pdb：切换在异常发生时自动进入调试器的开关。\n",
    "# %xmode：设置异常信息的详细程度（Plain/Context/Verbose）。\n",
    "# 运行、导入/导出与历史\n",
    "# %run script.py：在当前内核中运行外部 Python 脚本或 Notebook 片段（支持参数）。\n",
    "# %load 路径或URL：将外部脚本或网页片段载入到当前单元格中以便编辑/运行。\n",
    "# %%writefile 文件名：把整个单元格内容写入文件（常用来快速生成脚本/配置）。\n",
    "# %history：查看输入历史，支持筛选/输出到文件等选项。\n",
    "# %save：将历史中的某些输入行保存到文件。\n",
    "# 变量与会话管理\n",
    "# %who / %who_ls / %whos：列出当前命名空间中的变量，%whos 显示更详细的类型与大小信息。\n",
    "# %reset / %xdel：清空（或选择性清理）交互命名空间、彻底删除对象并尝试释放内存。\n",
    "# %store 变量名：在不同会话之间持久化/恢复变量（轻量跨会话传递）。\n",
    "# 环境与系统\n",
    "# %pwd / %cd：查看或切换当前工作目录。\n",
    "# %env：查看、设置环境变量（如 %env MY_VAR=value）。\n",
    "# %config：查看或设置各类交互式选项（如绘图后端、显示精度等）。\n",
    "# 显示与可视化\n",
    "# %matplotlib inline（或 widget 等）：设置 Matplotlib 的绘图后端（inline 将图嵌入输出）。\n",
    "# %%html / %%latex / %%javascript / %%svg：直接在输出中渲染 HTML、LaTeX、JS、SVG 内容。\n",
    "# %%capture：捕获单元格的标准输出/错误输出（可赋给变量以便后续处理）。\n",
    "# Shell/脚本与多语言\n",
    "# %%bash / %%sh / %%script：在整个单元格中以指定脚本语言运行（最常用的是 bash/sh）。\n",
    "# 扩展与生态\n",
    "# %load_ext / %unload_ext / %reload_ext：加载、卸载或重载 IPython 扩展，许多第三方扩展会提供额外的魔法命令（如上面的 line_profiler/memory_profiler）。\n",
    "# 提示：不同内核或环境中可用的魔法可能略有差异，先用 %lsmagic 检查你当前会话支持的命令集合。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "id": "b2399453",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "CPU times: total: 15.6 ms\n",
      "Wall time: 12.4 ms\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "<unknown>:1: SyntaxWarning: invalid escape sequence '\\p'\n"
     ]
    },
    {
     "data": {
      "application/vnd.microsoft.datawrangler.viewer.v0+json": {
       "columns": [
        {
         "name": "Day",
         "rawType": "datetime64[ns]",
         "type": "datetime"
        },
        {
         "name": "Preclose",
         "rawType": "object",
         "type": "string"
        },
        {
         "name": "Open",
         "rawType": "float64",
         "type": "float"
        },
        {
         "name": "Highest",
         "rawType": "float64",
         "type": "float"
        },
        {
         "name": "Lowest",
         "rawType": "float64",
         "type": "float"
        },
        {
         "name": "Close",
         "rawType": "float64",
         "type": "float"
        }
       ],
       "ref": "f64540f7-cdd2-433d-be5e-2957f15b1d84",
       "rows": [
        [
         "1990-12-19 00:00:00",
         "        ",
         "96.05",
         "99.98",
         "95.79",
         "99.98"
        ],
        [
         "1990-12-20 00:00:00",
         "99.98",
         "104.3",
         "104.39",
         "99.98",
         "104.39"
        ],
        [
         "1990-12-21 00:00:00",
         "104.39",
         "109.07",
         "109.13",
         "103.73",
         "109.13"
        ],
        [
         "1990-12-24 00:00:00",
         "109.13",
         "113.57",
         "114.55",
         "109.13",
         "114.55"
        ],
        [
         "1990-12-25 00:00:00",
         "114.55",
         "120.09",
         "120.25",
         "114.55",
         "120.25"
        ],
        [
         "1990-12-26 00:00:00",
         "120.25",
         "125.27",
         "125.27",
         "120.25",
         "125.27"
        ],
        [
         "1990-12-27 00:00:00",
         "125.27",
         "125.27",
         "125.28",
         "125.27",
         "125.28"
        ],
        [
         "1990-12-28 00:00:00",
         "125.28",
         "126.39",
         "126.45",
         "125.28",
         "126.45"
        ],
        [
         "1990-12-31 00:00:00",
         "126.45",
         "126.56",
         "127.61",
         "126.48",
         "127.61"
        ],
        [
         "1991-01-02 00:00:00",
         "127.61",
         "127.61",
         "128.84",
         "127.61",
         "128.84"
        ],
        [
         "1991-01-03 00:00:00",
         "128.84",
         "128.84",
         "130.14",
         "128.84",
         "130.14"
        ],
        [
         "1991-01-04 00:00:00",
         "130.14",
         "131.27",
         "131.44",
         "130.14",
         "131.44"
        ],
        [
         "1991-01-07 00:00:00",
         "131.44",
         "131.99",
         "132.06",
         "131.45",
         "132.06"
        ],
        [
         "1991-01-08 00:00:00",
         "132.06",
         "132.62",
         "132.68",
         "132.06",
         "132.68"
        ],
        [
         "1991-01-09 00:00:00",
         "132.68",
         "133.3",
         "133.34",
         "132.68",
         "133.34"
        ],
        [
         "1991-01-10 00:00:00",
         "133.34",
         "133.93",
         "133.97",
         "133.34",
         "133.97"
        ],
        [
         "1991-01-11 00:00:00",
         "133.97",
         "134.61",
         "134.61",
         "134.51",
         "134.6"
        ],
        [
         "1991-01-14 00:00:00",
         "134.6",
         "134.11",
         "135.19",
         "134.11",
         "134.67"
        ],
        [
         "1991-01-15 00:00:00",
         "134.67",
         "134.21",
         "134.74",
         "134.19",
         "134.74"
        ],
        [
         "1991-01-16 00:00:00",
         "134.74",
         "134.19",
         "134.74",
         "134.14",
         "134.24"
        ],
        [
         "1991-01-17 00:00:00",
         "134.24",
         "133.67",
         "134.25",
         "133.65",
         "134.25"
        ],
        [
         "1991-01-18 00:00:00",
         "134.25",
         "133.7",
         "134.25",
         "133.67",
         "134.24"
        ],
        [
         "1991-01-21 00:00:00",
         "134.24",
         "133.7",
         "134.24",
         "133.66",
         "134.24"
        ],
        [
         "1991-01-22 00:00:00",
         "134.24",
         "133.72",
         "134.24",
         "133.66",
         "133.72"
        ],
        [
         "1991-01-23 00:00:00",
         "133.72",
         "133.17",
         "133.72",
         "133.14",
         "133.17"
        ],
        [
         "1991-01-24 00:00:00",
         "133.17",
         "132.61",
         "133.17",
         "132.57",
         "132.61"
        ],
        [
         "1991-01-25 00:00:00",
         "132.61",
         "132.05",
         "132.07",
         "132.03",
         "132.05"
        ],
        [
         "1991-01-28 00:00:00",
         "132.05",
         "131.46",
         "131.55",
         "131.46",
         "131.46"
        ],
        [
         "1991-01-29 00:00:00",
         "131.46",
         "130.95",
         "130.97",
         "130.95",
         "130.95"
        ],
        [
         "1991-01-30 00:00:00",
         "130.95",
         "130.44",
         "130.95",
         "130.41",
         "130.44"
        ],
        [
         "1991-01-31 00:00:00",
         "130.44",
         "129.93",
         "130.46",
         "129.93",
         "129.97"
        ],
        [
         "1991-02-01 00:00:00",
         "129.97",
         "129.5",
         "129.97",
         "129.45",
         "129.51"
        ],
        [
         "1991-02-04 00:00:00",
         "129.51",
         "129.05",
         "129.58",
         "129.05",
         "129.05"
        ],
        [
         "1991-02-05 00:00:00",
         "129.05",
         "128.56",
         "128.58",
         "128.53",
         "128.58"
        ],
        [
         "1991-02-06 00:00:00",
         "128.58",
         "129.13",
         "129.15",
         "128.06",
         "129.14"
        ],
        [
         "1991-02-07 00:00:00",
         "129.14",
         "129.74",
         "129.79",
         "129.14",
         "129.79"
        ],
        [
         "1991-02-08 00:00:00",
         "129.79",
         "130.36",
         "130.39",
         "129.79",
         "130.38"
        ],
        [
         "1991-02-11 00:00:00",
         "130.38",
         "130.92",
         "130.97",
         "130.39",
         "130.97"
        ],
        [
         "1991-02-12 00:00:00",
         "130.97",
         "131.54",
         "131.56",
         "130.97",
         "131.35"
        ],
        [
         "1991-02-13 00:00:00",
         "131.35",
         "131.93",
         "131.93",
         "131.35",
         "131.92"
        ],
        [
         "1991-02-14 00:00:00",
         "131.92",
         "132.53",
         "132.53",
         "132.3",
         "132.53"
        ],
        [
         "1991-02-19 00:00:00",
         "132.53",
         "133.12",
         "133.14",
         "133.08",
         "133.13"
        ],
        [
         "1991-02-20 00:00:00",
         "133.13",
         "133.63",
         "133.67",
         "133.13",
         "133.67"
        ],
        [
         "1991-02-21 00:00:00",
         "133.67",
         "134.24",
         "134.28",
         "133.67",
         "134.28"
        ],
        [
         "1991-02-22 00:00:00",
         "134.28",
         "134.85",
         "134.87",
         "134.28",
         "134.87"
        ],
        [
         "1991-02-25 00:00:00",
         "134.87",
         "134.37",
         "134.87",
         "134.33",
         "134.4"
        ],
        [
         "1991-02-26 00:00:00",
         "134.4",
         "133.9",
         "134.44",
         "133.9",
         "133.93"
        ],
        [
         "1991-02-27 00:00:00",
         "133.93",
         "133.44",
         "133.98",
         "133.44",
         "133.47"
        ],
        [
         "1991-02-28 00:00:00",
         "133.47",
         "132.99",
         "133.52",
         "132.98",
         "133.01"
        ],
        [
         "1991-03-01 00:00:00",
         "133.01",
         "132.53",
         "132.53",
         "132.47",
         "132.53"
        ]
       ],
       "shape": {
        "columns": 5,
        "rows": 8473
       }
      },
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Preclose</th>\n",
       "      <th>Open</th>\n",
       "      <th>Highest</th>\n",
       "      <th>Lowest</th>\n",
       "      <th>Close</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Day</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>1990-12-19</th>\n",
       "      <td></td>\n",
       "      <td>96.050</td>\n",
       "      <td>99.980</td>\n",
       "      <td>95.790</td>\n",
       "      <td>99.980</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1990-12-20</th>\n",
       "      <td>99.98</td>\n",
       "      <td>104.300</td>\n",
       "      <td>104.390</td>\n",
       "      <td>99.980</td>\n",
       "      <td>104.390</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1990-12-21</th>\n",
       "      <td>104.39</td>\n",
       "      <td>109.070</td>\n",
       "      <td>109.130</td>\n",
       "      <td>103.730</td>\n",
       "      <td>109.130</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1990-12-24</th>\n",
       "      <td>109.13</td>\n",
       "      <td>113.570</td>\n",
       "      <td>114.550</td>\n",
       "      <td>109.130</td>\n",
       "      <td>114.550</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1990-12-25</th>\n",
       "      <td>114.55</td>\n",
       "      <td>120.090</td>\n",
       "      <td>120.250</td>\n",
       "      <td>114.550</td>\n",
       "      <td>120.250</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2025-08-25</th>\n",
       "      <td>3825.759</td>\n",
       "      <td>3848.163</td>\n",
       "      <td>3883.562</td>\n",
       "      <td>3839.972</td>\n",
       "      <td>3883.562</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2025-08-26</th>\n",
       "      <td>3883.562</td>\n",
       "      <td>3871.471</td>\n",
       "      <td>3888.599</td>\n",
       "      <td>3859.758</td>\n",
       "      <td>3868.382</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2025-08-27</th>\n",
       "      <td>3868.382</td>\n",
       "      <td>3869.612</td>\n",
       "      <td>3887.198</td>\n",
       "      <td>3800.350</td>\n",
       "      <td>3800.350</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2025-08-28</th>\n",
       "      <td>3800.35</td>\n",
       "      <td>3796.711</td>\n",
       "      <td>3845.087</td>\n",
       "      <td>3761.422</td>\n",
       "      <td>3843.597</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2025-08-29</th>\n",
       "      <td>3843.597</td>\n",
       "      <td>3842.823</td>\n",
       "      <td>3867.606</td>\n",
       "      <td>3839.206</td>\n",
       "      <td>3857.927</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>8473 rows × 5 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "            Preclose      Open   Highest    Lowest     Close\n",
       "Day                                                         \n",
       "1990-12-19              96.050    99.980    95.790    99.980\n",
       "1990-12-20     99.98   104.300   104.390    99.980   104.390\n",
       "1990-12-21    104.39   109.070   109.130   103.730   109.130\n",
       "1990-12-24    109.13   113.570   114.550   109.130   114.550\n",
       "1990-12-25    114.55   120.090   120.250   114.550   120.250\n",
       "...              ...       ...       ...       ...       ...\n",
       "2025-08-25  3825.759  3848.163  3883.562  3839.972  3883.562\n",
       "2025-08-26  3883.562  3871.471  3888.599  3859.758  3868.382\n",
       "2025-08-27  3868.382  3869.612  3887.198  3800.350  3800.350\n",
       "2025-08-28   3800.35  3796.711  3845.087  3761.422  3843.597\n",
       "2025-08-29  3843.597  3842.823  3867.606  3839.206  3857.927\n",
       "\n",
       "[8473 rows x 5 columns]"
      ]
     },
     "execution_count": 35,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "%%time\n",
    "data = pd.read_csv('D:\\python course/000001.csv')\n",
    "data['Day'] = pd.to_datetime(data['Day'],format='%Y/%m/%d')\n",
    "data.set_index('Day', inplace = True)\n",
    "data.sort_values(by = ['Day'],axis=0, ascending=True)\n",
    "data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "id": "6d594f61",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 收益率计算基础 (Return Calculation Basics)\n",
    "# 股票的收益率的计算一般使用的是 收盘价 来计算。\n",
    "# Raw Return: Rt=pt−pt−1pt−1\n",
    "# Log Return: rt=log(pt)−log(pt−1)\n",
    "# 自然对数收益率与原始收益率的关系：\n",
    "# rt=log(1+Rt)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "id": "ffbb30e7",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.microsoft.datawrangler.viewer.v0+json": {
       "columns": [
        {
         "name": "Day",
         "rawType": "datetime64[ns]",
         "type": "datetime"
        },
        {
         "name": "Preclose",
         "rawType": "float64",
         "type": "float"
        },
        {
         "name": "Open",
         "rawType": "float64",
         "type": "float"
        },
        {
         "name": "Highest",
         "rawType": "float64",
         "type": "float"
        },
        {
         "name": "Lowest",
         "rawType": "float64",
         "type": "float"
        },
        {
         "name": "Close",
         "rawType": "float64",
         "type": "float"
        }
       ],
       "ref": "3f46893a-ee27-4fe6-841d-0683fba26a7e",
       "rows": [
        [
         "1995-01-03 00:00:00",
         "647.87",
         "637.72",
         "647.71",
         "630.53",
         "639.88"
        ],
        [
         "1995-01-04 00:00:00",
         "639.88",
         "641.9",
         "655.51",
         "638.86",
         "653.81"
        ],
        [
         "1995-01-05 00:00:00",
         "653.81",
         "655.38",
         "657.52",
         "645.81",
         "646.89"
        ],
        [
         "1995-01-06 00:00:00",
         "646.89",
         "642.75",
         "643.89",
         "636.33",
         "640.76"
        ],
        [
         "1995-01-09 00:00:00",
         "640.76",
         "637.52",
         "637.55",
         "625.04",
         "626.0"
        ],
        [
         "1995-01-10 00:00:00",
         "626.0",
         "616.16",
         "617.6",
         "607.06",
         "610.3"
        ],
        [
         "1995-01-11 00:00:00",
         "610.3",
         "610.3",
         "615.21",
         "606.92",
         "613.13"
        ],
        [
         "1995-01-12 00:00:00",
         "613.13",
         "612.79",
         "615.47",
         "601.17",
         "602.12"
        ],
        [
         "1995-01-13 00:00:00",
         "602.12",
         "597.32",
         "601.07",
         "593.81",
         "597.84"
        ],
        [
         "1995-01-16 00:00:00",
         "597.84",
         "591.06",
         "591.32",
         "584.88",
         "588.81"
        ],
        [
         "1995-01-17 00:00:00",
         "588.81",
         "591.81",
         "595.02",
         "586.6",
         "587.94"
        ],
        [
         "1995-01-18 00:00:00",
         "587.94",
         "588.53",
         "593.65",
         "586.99",
         "593.64"
        ],
        [
         "1995-01-19 00:00:00",
         "593.64",
         "589.7",
         "593.1",
         "571.36",
         "571.72"
        ],
        [
         "1995-01-20 00:00:00",
         "571.72",
         "566.81",
         "578.53",
         "559.66",
         "571.24"
        ],
        [
         "1995-01-23 00:00:00",
         "571.24",
         "564.86",
         "565.04",
         "553.11",
         "553.44"
        ],
        [
         "1995-01-24 00:00:00",
         "553.44",
         "552.78",
         "559.34",
         "547.38",
         "547.57"
        ],
        [
         "1995-01-25 00:00:00",
         "547.57",
         "550.27",
         "558.58",
         "550.27",
         "555.77"
        ],
        [
         "1995-01-26 00:00:00",
         "555.77",
         "553.91",
         "555.26",
         "551.14",
         "553.11"
        ],
        [
         "1995-01-27 00:00:00",
         "553.11",
         "552.95",
         "564.61",
         "552.93",
         "562.59"
        ],
        [
         "1995-02-06 00:00:00",
         "562.59",
         "559.78",
         "559.84",
         "532.91",
         "533.72"
        ],
        [
         "1995-02-07 00:00:00",
         "533.72",
         "532.11",
         "536.63",
         "524.43",
         "532.49"
        ],
        [
         "1995-02-08 00:00:00",
         "532.49",
         "533.09",
         "537.55",
         "531.22",
         "533.75"
        ],
        [
         "1995-02-09 00:00:00",
         "533.75",
         "533.79",
         "553.36",
         "533.14",
         "553.36"
        ],
        [
         "1995-02-10 00:00:00",
         "553.36",
         "548.77",
         "558.95",
         "540.63",
         "541.38"
        ],
        [
         "1995-02-13 00:00:00",
         "541.38",
         "537.95",
         "550.25",
         "537.05",
         "548.84"
        ],
        [
         "1995-02-14 00:00:00",
         "548.84",
         "549.76",
         "552.35",
         "542.18",
         "548.11"
        ],
        [
         "1995-02-15 00:00:00",
         "548.11",
         "547.83",
         "548.69",
         "542.5",
         "542.84"
        ],
        [
         "1995-02-16 00:00:00",
         "542.84",
         "542.26",
         "542.82",
         "537.21",
         "538.37"
        ],
        [
         "1995-02-17 00:00:00",
         "538.37",
         "537.87",
         "537.91",
         "532.67",
         "533.76"
        ],
        [
         "1995-02-20 00:00:00",
         "533.76",
         "534.57",
         "540.86",
         "534.51",
         "534.96"
        ],
        [
         "1995-02-21 00:00:00",
         "534.96",
         "536.07",
         "541.14",
         "535.97",
         "541.14"
        ],
        [
         "1995-02-22 00:00:00",
         "541.14",
         "541.21",
         "594.81",
         "541.21",
         "594.64"
        ],
        [
         "1995-02-23 00:00:00",
         "594.64",
         "589.9",
         "611.89",
         "575.82",
         "589.13"
        ],
        [
         "1995-02-24 00:00:00",
         "589.13",
         "591.91",
         "594.92",
         "555.2",
         "558.43"
        ],
        [
         "1995-02-27 00:00:00",
         "558.43",
         "584.02",
         "591.43",
         "559.04",
         "561.27"
        ],
        [
         "1995-02-28 00:00:00",
         "561.27",
         "558.42",
         "558.42",
         "544.06",
         "549.26"
        ],
        [
         "1995-03-01 00:00:00",
         "549.26",
         "548.91",
         "559.48",
         "545.72",
         "556.96"
        ],
        [
         "1995-03-02 00:00:00",
         "556.96",
         "557.5",
         "564.64",
         "553.52",
         "560.85"
        ],
        [
         "1995-03-03 00:00:00",
         "560.85",
         "562.3",
         "580.39",
         "562.3",
         "577.22"
        ],
        [
         "1995-03-06 00:00:00",
         "577.22",
         "585.24",
         "616.76",
         "582.55",
         "616.3"
        ],
        [
         "1995-03-07 00:00:00",
         "616.3",
         "614.73",
         "635.24",
         "597.2",
         "632.96"
        ],
        [
         "1995-03-08 00:00:00",
         "632.96",
         "632.96",
         "634.2",
         "618.8",
         "620.81"
        ],
        [
         "1995-03-09 00:00:00",
         "620.81",
         "617.22",
         "624.25",
         "604.79",
         "605.59"
        ],
        [
         "1995-03-10 00:00:00",
         "605.59",
         "600.45",
         "600.51",
         "583.72",
         "586.29"
        ],
        [
         "1995-03-13 00:00:00",
         "586.29",
         "596.65",
         "597.18",
         "583.38",
         "586.23"
        ],
        [
         "1995-03-14 00:00:00",
         "586.23",
         "588.18",
         "599.9",
         "586.19",
         "588.24"
        ],
        [
         "1995-03-15 00:00:00",
         "588.24",
         "588.78",
         "591.49",
         "568.6",
         "572.6"
        ],
        [
         "1995-03-16 00:00:00",
         "572.6",
         "570.51",
         "579.88",
         "569.17",
         "579.88"
        ],
        [
         "1995-03-17 00:00:00",
         "579.88",
         "579.62",
         "593.97",
         "577.91",
         "593.52"
        ],
        [
         "1995-03-20 00:00:00",
         "593.52",
         "597.85",
         "604.96",
         "585.15",
         "585.45"
        ]
       ],
       "shape": {
        "columns": 5,
        "rows": 7445
       }
      },
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Preclose</th>\n",
       "      <th>Open</th>\n",
       "      <th>Highest</th>\n",
       "      <th>Lowest</th>\n",
       "      <th>Close</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Day</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>1995-01-03</th>\n",
       "      <td>647.870</td>\n",
       "      <td>637.720</td>\n",
       "      <td>647.710</td>\n",
       "      <td>630.530</td>\n",
       "      <td>639.880</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1995-01-04</th>\n",
       "      <td>639.880</td>\n",
       "      <td>641.900</td>\n",
       "      <td>655.510</td>\n",
       "      <td>638.860</td>\n",
       "      <td>653.810</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1995-01-05</th>\n",
       "      <td>653.810</td>\n",
       "      <td>655.380</td>\n",
       "      <td>657.520</td>\n",
       "      <td>645.810</td>\n",
       "      <td>646.890</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1995-01-06</th>\n",
       "      <td>646.890</td>\n",
       "      <td>642.750</td>\n",
       "      <td>643.890</td>\n",
       "      <td>636.330</td>\n",
       "      <td>640.760</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1995-01-09</th>\n",
       "      <td>640.760</td>\n",
       "      <td>637.520</td>\n",
       "      <td>637.550</td>\n",
       "      <td>625.040</td>\n",
       "      <td>626.000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2025-08-25</th>\n",
       "      <td>3825.759</td>\n",
       "      <td>3848.163</td>\n",
       "      <td>3883.562</td>\n",
       "      <td>3839.972</td>\n",
       "      <td>3883.562</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2025-08-26</th>\n",
       "      <td>3883.562</td>\n",
       "      <td>3871.471</td>\n",
       "      <td>3888.599</td>\n",
       "      <td>3859.758</td>\n",
       "      <td>3868.382</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2025-08-27</th>\n",
       "      <td>3868.382</td>\n",
       "      <td>3869.612</td>\n",
       "      <td>3887.198</td>\n",
       "      <td>3800.350</td>\n",
       "      <td>3800.350</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2025-08-28</th>\n",
       "      <td>3800.350</td>\n",
       "      <td>3796.711</td>\n",
       "      <td>3845.087</td>\n",
       "      <td>3761.422</td>\n",
       "      <td>3843.597</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2025-08-29</th>\n",
       "      <td>3843.597</td>\n",
       "      <td>3842.823</td>\n",
       "      <td>3867.606</td>\n",
       "      <td>3839.206</td>\n",
       "      <td>3857.927</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>7445 rows × 5 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "            Preclose      Open   Highest    Lowest     Close\n",
       "Day                                                         \n",
       "1995-01-03   647.870   637.720   647.710   630.530   639.880\n",
       "1995-01-04   639.880   641.900   655.510   638.860   653.810\n",
       "1995-01-05   653.810   655.380   657.520   645.810   646.890\n",
       "1995-01-06   646.890   642.750   643.890   636.330   640.760\n",
       "1995-01-09   640.760   637.520   637.550   625.040   626.000\n",
       "...              ...       ...       ...       ...       ...\n",
       "2025-08-25  3825.759  3848.163  3883.562  3839.972  3883.562\n",
       "2025-08-26  3883.562  3871.471  3888.599  3859.758  3868.382\n",
       "2025-08-27  3868.382  3869.612  3887.198  3800.350  3800.350\n",
       "2025-08-28  3800.350  3796.711  3845.087  3761.422  3843.597\n",
       "2025-08-29  3843.597  3842.823  3867.606  3839.206  3857.927\n",
       "\n",
       "[7445 rows x 5 columns]"
      ]
     },
     "execution_count": 37,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data_new = data['1995-01':'2025-08'].copy()\n",
    "data_new['Close'] = pd.to_numeric(data_new['Close'])\n",
    "data_new['Preclose'] = pd.to_numeric(data_new['Preclose'])\n",
    "data_new\n",
    "#日度收益率计算数据准备"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "id": "5403cc99",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 计算000001上证指数日收益率 - 方法1：直接使用向量化操作（最推荐的方式）\n",
    "data_new['Raw_return'] = data_new['Close'] / data_new['Preclose'] - 1\n",
    "data_new['Log_return'] = np.log(data_new['Close']) - np.log(data_new['Preclose'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "id": "7fcb5f66",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 方法2：使用pandas的pct_change函数计算收益率（适用于时间序列数据）\n",
    "# 注意：这种方法需要数据已经按时间排序\n",
    "data_new['Pct_change_return'] = data_new['Close'].pct_change()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "id": "4ebde0b7",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 方法3：使用apply方法（不推荐，因为速度较慢）\n",
    "data_new['Apply_return'] = data_new.apply(lambda row: row['Close'] / row['Preclose'] - 1, axis=1)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "id": "30e7a6c8",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 方法4：使用diff和div方法组合（另一种向量化操作）\n",
    "data_new['Diff_div_return'] = data_new['Close'].diff() / data_new['Close'].shift(1)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "id": "36696e83",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.microsoft.datawrangler.viewer.v0+json": {
       "columns": [
        {
         "name": "Day",
         "rawType": "datetime64[ns]",
         "type": "datetime"
        },
        {
         "name": "Preclose",
         "rawType": "float64",
         "type": "float"
        },
        {
         "name": "Open",
         "rawType": "float64",
         "type": "float"
        },
        {
         "name": "Highest",
         "rawType": "float64",
         "type": "float"
        },
        {
         "name": "Lowest",
         "rawType": "float64",
         "type": "float"
        },
        {
         "name": "Close",
         "rawType": "float64",
         "type": "float"
        },
        {
         "name": "Raw_return",
         "rawType": "float64",
         "type": "float"
        },
        {
         "name": "Log_return",
         "rawType": "float64",
         "type": "float"
        },
        {
         "name": "Pct_change_return",
         "rawType": "float64",
         "type": "float"
        },
        {
         "name": "Apply_return",
         "rawType": "float64",
         "type": "float"
        },
        {
         "name": "Diff_div_return",
         "rawType": "float64",
         "type": "float"
        }
       ],
       "ref": "ec94b1fa-ae04-40e9-9286-149ab0898235",
       "rows": [
        [
         "1995-01-03 00:00:00",
         "647.87",
         "637.72",
         "647.71",
         "630.53",
         "639.88",
         "-0.01233272107058514",
         "-0.012409400168589713",
         null,
         "-0.01233272107058514",
         null
        ],
        [
         "1995-01-04 00:00:00",
         "639.88",
         "641.9",
         "655.51",
         "638.86",
         "653.81",
         "0.021769706820028656",
         "0.021536130597052505",
         "0.021769706820028656",
         "0.021769706820028656",
         "0.021769706820028677"
        ],
        [
         "1995-01-05 00:00:00",
         "653.81",
         "655.38",
         "657.52",
         "645.81",
         "646.89",
         "-0.010584114651045362",
         "-0.010640524779764071",
         "-0.010584114651045362",
         "-0.010584114651045362",
         "-0.010584114651045348"
        ],
        [
         "1995-01-06 00:00:00",
         "646.89",
         "642.75",
         "643.89",
         "636.33",
         "640.76",
         "-0.00947610876656002",
         "-0.009521292757400879",
         "-0.00947610876656002",
         "-0.00947610876656002",
         "-0.009476108766559996"
        ],
        [
         "1995-01-09 00:00:00",
         "640.76",
         "637.52",
         "637.55",
         "625.04",
         "626.0",
         "-0.023035145764404796",
         "-0.023304600733184522",
         "-0.023035145764404796",
         "-0.023035145764404796",
         "-0.023035145764404755"
        ],
        [
         "1995-01-10 00:00:00",
         "626.0",
         "616.16",
         "617.6",
         "607.06",
         "610.3",
         "-0.02507987220447294",
         "-0.025399731549649474",
         "-0.02507987220447294",
         "-0.02507987220447294",
         "-0.025079872204472917"
        ],
        [
         "1995-01-11 00:00:00",
         "610.3",
         "610.3",
         "615.21",
         "606.92",
         "613.13",
         "0.004637063739144853",
         "0.004626345679861643",
         "0.004637063739144853",
         "0.004637063739144853",
         "0.00463706373914475"
        ],
        [
         "1995-01-12 00:00:00",
         "613.13",
         "612.79",
         "615.47",
         "601.17",
         "602.12",
         "-0.017957040105687216",
         "-0.01812022423800741",
         "-0.017957040105687216",
         "-0.017957040105687216",
         "-0.017957040105687196"
        ],
        [
         "1995-01-13 00:00:00",
         "602.12",
         "597.32",
         "601.07",
         "593.81",
         "597.84",
         "-0.007108217631036973",
         "-0.0071336013702687495",
         "-0.007108217631036973",
         "-0.007108217631036973",
         "-0.007108217631036957"
        ],
        [
         "1995-01-16 00:00:00",
         "597.84",
         "591.06",
         "591.32",
         "584.88",
         "588.81",
         "-0.015104375752709953",
         "-0.0152196086559373",
         "-0.015104375752709953",
         "-0.015104375752709953",
         "-0.0151043757527099"
        ],
        [
         "1995-01-17 00:00:00",
         "588.81",
         "591.81",
         "595.02",
         "586.6",
         "587.94",
         "-0.0014775564273702413",
         "-0.0014786490903144767",
         "-0.0014775564273702413",
         "-0.0014775564273702413",
         "-0.0014775564273702738"
        ],
        [
         "1995-01-18 00:00:00",
         "587.94",
         "588.53",
         "593.65",
         "586.99",
         "593.64",
         "0.009694866823145132",
         "0.009648173151834527",
         "0.009694866823145132",
         "0.009694866823145132",
         "0.009694866823145102"
        ],
        [
         "1995-01-19 00:00:00",
         "593.64",
         "589.7",
         "593.1",
         "571.36",
         "571.72",
         "-0.03692473552995079",
         "-0.03762371398670261",
         "-0.03692473552995079",
         "-0.03692473552995079",
         "-0.03692473552995074"
        ],
        [
         "1995-01-20 00:00:00",
         "571.72",
         "566.81",
         "578.53",
         "559.66",
         "571.24",
         "-0.0008395718183726864",
         "-0.000839924456181862",
         "-0.0008395718183726864",
         "-0.0008395718183726864",
         "-0.0008395718183726617"
        ],
        [
         "1995-01-23 00:00:00",
         "571.24",
         "564.86",
         "565.04",
         "553.11",
         "553.44",
         "-0.031160282893354707",
         "-0.03165609139587389",
         "-0.031160282893354707",
         "-0.031160282893354707",
         "-0.031160282893354728"
        ],
        [
         "1995-01-24 00:00:00",
         "553.44",
         "552.78",
         "559.34",
         "547.38",
         "547.57",
         "-0.01060638912980627",
         "-0.010663037789535146",
         "-0.01060638912980627",
         "-0.01060638912980627",
         "-0.010606389129806309"
        ],
        [
         "1995-01-25 00:00:00",
         "547.57",
         "550.27",
         "558.58",
         "550.27",
         "555.77",
         "0.014975254305385377",
         "0.014864232201859728",
         "0.014975254305385377",
         "0.014975254305385377",
         "0.014975254305385486"
        ],
        [
         "1995-01-26 00:00:00",
         "555.77",
         "553.91",
         "555.26",
         "551.14",
         "553.11",
         "-0.004786152545117495",
         "-0.004797642850774331",
         "-0.004786152545117495",
         "-0.004786152545117495",
         "-0.004786152545117528"
        ],
        [
         "1995-01-27 00:00:00",
         "553.11",
         "552.95",
         "564.61",
         "552.93",
         "562.59",
         "0.01713944784943333",
         "0.016994224529531365",
         "0.01713944784943333",
         "0.01713944784943333",
         "0.017139447849433238"
        ],
        [
         "1995-02-06 00:00:00",
         "562.59",
         "559.78",
         "559.84",
         "532.91",
         "533.72",
         "-0.05131623384702888",
         "-0.05267976440572042",
         "-0.05131623384702888",
         "-0.05131623384702888",
         "-0.05131623384702892"
        ],
        [
         "1995-02-07 00:00:00",
         "533.72",
         "532.11",
         "536.63",
         "524.43",
         "532.49",
         "-0.0023045791800944126",
         "-0.0023072388096965213",
         "-0.0023045791800944126",
         "-0.0023045791800944126",
         "-0.0023045791800944656"
        ],
        [
         "1995-02-08 00:00:00",
         "532.49",
         "533.09",
         "537.55",
         "531.22",
         "533.75",
         "0.0023662416195608937",
         "0.002363446478311104",
         "0.0023662416195608937",
         "0.0023662416195608937",
         "0.0023662416195609137"
        ],
        [
         "1995-02-09 00:00:00",
         "533.75",
         "533.79",
         "553.36",
         "533.14",
         "553.36",
         "0.03674004683840759",
         "0.03608121974949441",
         "0.03674004683840759",
         "0.03674004683840759",
         "0.03674004683840752"
        ],
        [
         "1995-02-10 00:00:00",
         "553.36",
         "548.77",
         "558.95",
         "540.63",
         "541.38",
         "-0.02164955905739485",
         "-0.021887349057864824",
         "-0.02164955905739485",
         "-0.02164955905739485",
         "-0.021649559057394855"
        ],
        [
         "1995-02-13 00:00:00",
         "541.38",
         "537.95",
         "550.25",
         "537.05",
         "548.84",
         "0.01377960028076397",
         "0.013685524818510153",
         "0.01377960028076397",
         "0.01377960028076397",
         "0.013779600280764042"
        ],
        [
         "1995-02-14 00:00:00",
         "548.84",
         "549.76",
         "552.35",
         "542.18",
         "548.11",
         "-0.0013300779826543874",
         "-0.0013309633215072836",
         "-0.0013300779826543874",
         "-0.0013300779826543874",
         "-0.0013300779826543585"
        ],
        [
         "1995-02-15 00:00:00",
         "548.11",
         "547.83",
         "548.69",
         "542.5",
         "542.84",
         "-0.00961485833135678",
         "-0.009661379518297153",
         "-0.00961485833135678",
         "-0.00961485833135678",
         "-0.00961485833135681"
        ],
        [
         "1995-02-16 00:00:00",
         "542.84",
         "542.26",
         "542.82",
         "537.21",
         "538.37",
         "-0.008234470562228369",
         "-0.008268561088893733",
         "-0.008234470562228369",
         "-0.008234470562228369",
         "-0.00823447056222833"
        ],
        [
         "1995-02-17 00:00:00",
         "538.37",
         "537.87",
         "537.91",
         "532.67",
         "533.76",
         "-0.008562884261753112",
         "-0.00859975639394861",
         "-0.008562884261753112",
         "-0.008562884261753112",
         "-0.008562884261753095"
        ],
        [
         "1995-02-20 00:00:00",
         "533.76",
         "534.57",
         "540.86",
         "534.51",
         "534.96",
         "0.0022482014388489624",
         "0.002245678015396102",
         "0.0022482014388489624",
         "0.0022482014388489624",
         "0.002248201438849006"
        ],
        [
         "1995-02-21 00:00:00",
         "534.96",
         "536.07",
         "541.14",
         "535.97",
         "541.14",
         "0.011552265589950483",
         "0.01148604765994854",
         "0.011552265589950483",
         "0.011552265589950483",
         "0.011552265589950556"
        ],
        [
         "1995-02-22 00:00:00",
         "541.14",
         "541.21",
         "594.81",
         "541.21",
         "594.64",
         "0.09886535831762577",
         "0.0942781550113656",
         "0.09886535831762577",
         "0.09886535831762577",
         "0.09886535831762576"
        ],
        [
         "1995-02-23 00:00:00",
         "594.64",
         "589.9",
         "611.89",
         "575.82",
         "589.13",
         "-0.009266110587918708",
         "-0.00930930804599317",
         "-0.009266110587918708",
         "-0.009266110587918708",
         "-0.009266110587918725"
        ],
        [
         "1995-02-24 00:00:00",
         "589.13",
         "591.91",
         "594.92",
         "555.2",
         "558.43",
         "-0.05211073956512158",
         "-0.05351759743764717",
         "-0.05211073956512158",
         "-0.05211073956512158",
         "-0.05211073956512153"
        ],
        [
         "1995-02-27 00:00:00",
         "558.43",
         "584.02",
         "591.43",
         "559.04",
         "561.27",
         "0.005085686657235522",
         "0.005072798232039766",
         "0.005085686657235522",
         "0.005085686657235522",
         "0.005085686657235521"
        ],
        [
         "1995-02-28 00:00:00",
         "561.27",
         "558.42",
         "558.42",
         "544.06",
         "549.26",
         "-0.021397901188376367",
         "-0.02163015542103075",
         "-0.021397901188376367",
         "-0.021397901188376367",
         "-0.021397901188376346"
        ],
        [
         "1995-03-01 00:00:00",
         "549.26",
         "548.91",
         "559.48",
         "545.72",
         "556.96",
         "0.014018861741251998",
         "0.013921506318722798",
         "0.014018861741251998",
         "0.014018861741251998",
         "0.01401886174125195"
        ],
        [
         "1995-03-02 00:00:00",
         "556.96",
         "557.5",
         "564.64",
         "553.52",
         "560.85",
         "0.006984343579431229",
         "0.0069600660281032845",
         "0.006984343579431229",
         "0.006984343579431229",
         "0.006984343579431173"
        ],
        [
         "1995-03-03 00:00:00",
         "560.85",
         "562.3",
         "580.39",
         "562.3",
         "577.22",
         "0.02918783988588758",
         "0.028769986242834555",
         "0.02918783988588758",
         "0.02918783988588758",
         "0.0291878398858875"
        ],
        [
         "1995-03-06 00:00:00",
         "577.22",
         "585.24",
         "616.76",
         "582.55",
         "616.3",
         "0.06770382176639744",
         "0.06551038163413025",
         "0.06770382176639744",
         "0.06770382176639744",
         "0.06770382176639743"
        ],
        [
         "1995-03-07 00:00:00",
         "616.3",
         "614.73",
         "635.24",
         "597.2",
         "632.96",
         "0.027032289469414383",
         "0.026673371026095793",
         "0.027032289469414383",
         "0.027032289469414383",
         "0.02703228946941438"
        ],
        [
         "1995-03-08 00:00:00",
         "632.96",
         "632.96",
         "634.2",
         "618.8",
         "620.81",
         "-0.019195525783619938",
         "-0.019382152007597853",
         "-0.019195525783619938",
         "-0.019195525783619938",
         "-0.019195525783619962"
        ],
        [
         "1995-03-09 00:00:00",
         "620.81",
         "617.22",
         "624.25",
         "604.79",
         "605.59",
         "-0.0245163576617643",
         "-0.02482188754932757",
         "-0.0245163576617643",
         "-0.0245163576617643",
         "-0.024516357661764333"
        ],
        [
         "1995-03-10 00:00:00",
         "605.59",
         "600.45",
         "600.51",
         "583.72",
         "586.29",
         "-0.03186974685843569",
         "-0.032388641727272294",
         "-0.03186974685843569",
         "-0.03186974685843569",
         "-0.03186974685843569"
        ],
        [
         "1995-03-13 00:00:00",
         "586.29",
         "596.65",
         "597.18",
         "583.38",
         "586.23",
         "-0.00010233843319851221",
         "-0.00010234367013328693",
         "-0.00010233843319851221",
         "-0.00010233843319851221",
         "-0.00010233843319849466"
        ],
        [
         "1995-03-14 00:00:00",
         "586.23",
         "588.18",
         "599.9",
         "586.19",
         "588.24",
         "0.003428688398751323",
         "0.0034228238480045192",
         "0.003428688398751323",
         "0.003428688398751323",
         "0.0034286883987513276"
        ],
        [
         "1995-03-15 00:00:00",
         "588.24",
         "588.78",
         "591.49",
         "568.6",
         "572.6",
         "-0.026587787297701615",
         "-0.026947635223951494",
         "-0.026587787297701615",
         "-0.026587787297701615",
         "-0.026587787297701594"
        ],
        [
         "1995-03-16 00:00:00",
         "572.6",
         "570.51",
         "579.88",
         "569.17",
         "579.88",
         "0.012713936430317707",
         "0.012633792918681941",
         "0.012713936430317707",
         "0.012713936430317707",
         "0.0127139364303178"
        ],
        [
         "1995-03-17 00:00:00",
         "579.88",
         "579.62",
         "593.97",
         "577.91",
         "593.52",
         "0.02352210802234933",
         "0.023249726298573137",
         "0.02352210802234933",
         "0.02352210802234933",
         "0.023522108022349428"
        ],
        [
         "1995-03-20 00:00:00",
         "593.52",
         "597.85",
         "604.96",
         "585.15",
         "585.45",
         "-0.01359684593610988",
         "-0.013690129586536592",
         "-0.01359684593610988",
         "-0.01359684593610988",
         "-0.01359684593610988"
        ]
       ],
       "shape": {
        "columns": 10,
        "rows": 7445
       }
      },
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Preclose</th>\n",
       "      <th>Open</th>\n",
       "      <th>Highest</th>\n",
       "      <th>Lowest</th>\n",
       "      <th>Close</th>\n",
       "      <th>Raw_return</th>\n",
       "      <th>Log_return</th>\n",
       "      <th>Pct_change_return</th>\n",
       "      <th>Apply_return</th>\n",
       "      <th>Diff_div_return</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Day</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>1995-01-03</th>\n",
       "      <td>647.870</td>\n",
       "      <td>637.720</td>\n",
       "      <td>647.710</td>\n",
       "      <td>630.530</td>\n",
       "      <td>639.880</td>\n",
       "      <td>-0.012333</td>\n",
       "      <td>-0.012409</td>\n",
       "      <td>NaN</td>\n",
       "      <td>-0.012333</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1995-01-04</th>\n",
       "      <td>639.880</td>\n",
       "      <td>641.900</td>\n",
       "      <td>655.510</td>\n",
       "      <td>638.860</td>\n",
       "      <td>653.810</td>\n",
       "      <td>0.021770</td>\n",
       "      <td>0.021536</td>\n",
       "      <td>0.021770</td>\n",
       "      <td>0.021770</td>\n",
       "      <td>0.021770</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1995-01-05</th>\n",
       "      <td>653.810</td>\n",
       "      <td>655.380</td>\n",
       "      <td>657.520</td>\n",
       "      <td>645.810</td>\n",
       "      <td>646.890</td>\n",
       "      <td>-0.010584</td>\n",
       "      <td>-0.010641</td>\n",
       "      <td>-0.010584</td>\n",
       "      <td>-0.010584</td>\n",
       "      <td>-0.010584</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1995-01-06</th>\n",
       "      <td>646.890</td>\n",
       "      <td>642.750</td>\n",
       "      <td>643.890</td>\n",
       "      <td>636.330</td>\n",
       "      <td>640.760</td>\n",
       "      <td>-0.009476</td>\n",
       "      <td>-0.009521</td>\n",
       "      <td>-0.009476</td>\n",
       "      <td>-0.009476</td>\n",
       "      <td>-0.009476</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1995-01-09</th>\n",
       "      <td>640.760</td>\n",
       "      <td>637.520</td>\n",
       "      <td>637.550</td>\n",
       "      <td>625.040</td>\n",
       "      <td>626.000</td>\n",
       "      <td>-0.023035</td>\n",
       "      <td>-0.023305</td>\n",
       "      <td>-0.023035</td>\n",
       "      <td>-0.023035</td>\n",
       "      <td>-0.023035</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2025-08-25</th>\n",
       "      <td>3825.759</td>\n",
       "      <td>3848.163</td>\n",
       "      <td>3883.562</td>\n",
       "      <td>3839.972</td>\n",
       "      <td>3883.562</td>\n",
       "      <td>0.015109</td>\n",
       "      <td>0.014996</td>\n",
       "      <td>0.015109</td>\n",
       "      <td>0.015109</td>\n",
       "      <td>0.015109</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2025-08-26</th>\n",
       "      <td>3883.562</td>\n",
       "      <td>3871.471</td>\n",
       "      <td>3888.599</td>\n",
       "      <td>3859.758</td>\n",
       "      <td>3868.382</td>\n",
       "      <td>-0.003909</td>\n",
       "      <td>-0.003916</td>\n",
       "      <td>-0.003909</td>\n",
       "      <td>-0.003909</td>\n",
       "      <td>-0.003909</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2025-08-27</th>\n",
       "      <td>3868.382</td>\n",
       "      <td>3869.612</td>\n",
       "      <td>3887.198</td>\n",
       "      <td>3800.350</td>\n",
       "      <td>3800.350</td>\n",
       "      <td>-0.017587</td>\n",
       "      <td>-0.017743</td>\n",
       "      <td>-0.017587</td>\n",
       "      <td>-0.017587</td>\n",
       "      <td>-0.017587</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2025-08-28</th>\n",
       "      <td>3800.350</td>\n",
       "      <td>3796.711</td>\n",
       "      <td>3845.087</td>\n",
       "      <td>3761.422</td>\n",
       "      <td>3843.597</td>\n",
       "      <td>0.011380</td>\n",
       "      <td>0.011315</td>\n",
       "      <td>0.011380</td>\n",
       "      <td>0.011380</td>\n",
       "      <td>0.011380</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2025-08-29</th>\n",
       "      <td>3843.597</td>\n",
       "      <td>3842.823</td>\n",
       "      <td>3867.606</td>\n",
       "      <td>3839.206</td>\n",
       "      <td>3857.927</td>\n",
       "      <td>0.003728</td>\n",
       "      <td>0.003721</td>\n",
       "      <td>0.003728</td>\n",
       "      <td>0.003728</td>\n",
       "      <td>0.003728</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>7445 rows × 10 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "            Preclose      Open   Highest    Lowest     Close  Raw_return  \\\n",
       "Day                                                                        \n",
       "1995-01-03   647.870   637.720   647.710   630.530   639.880   -0.012333   \n",
       "1995-01-04   639.880   641.900   655.510   638.860   653.810    0.021770   \n",
       "1995-01-05   653.810   655.380   657.520   645.810   646.890   -0.010584   \n",
       "1995-01-06   646.890   642.750   643.890   636.330   640.760   -0.009476   \n",
       "1995-01-09   640.760   637.520   637.550   625.040   626.000   -0.023035   \n",
       "...              ...       ...       ...       ...       ...         ...   \n",
       "2025-08-25  3825.759  3848.163  3883.562  3839.972  3883.562    0.015109   \n",
       "2025-08-26  3883.562  3871.471  3888.599  3859.758  3868.382   -0.003909   \n",
       "2025-08-27  3868.382  3869.612  3887.198  3800.350  3800.350   -0.017587   \n",
       "2025-08-28  3800.350  3796.711  3845.087  3761.422  3843.597    0.011380   \n",
       "2025-08-29  3843.597  3842.823  3867.606  3839.206  3857.927    0.003728   \n",
       "\n",
       "            Log_return  Pct_change_return  Apply_return  Diff_div_return  \n",
       "Day                                                                       \n",
       "1995-01-03   -0.012409                NaN     -0.012333              NaN  \n",
       "1995-01-04    0.021536           0.021770      0.021770         0.021770  \n",
       "1995-01-05   -0.010641          -0.010584     -0.010584        -0.010584  \n",
       "1995-01-06   -0.009521          -0.009476     -0.009476        -0.009476  \n",
       "1995-01-09   -0.023305          -0.023035     -0.023035        -0.023035  \n",
       "...                ...                ...           ...              ...  \n",
       "2025-08-25    0.014996           0.015109      0.015109         0.015109  \n",
       "2025-08-26   -0.003916          -0.003909     -0.003909        -0.003909  \n",
       "2025-08-27   -0.017743          -0.017587     -0.017587        -0.017587  \n",
       "2025-08-28    0.011315           0.011380      0.011380         0.011380  \n",
       "2025-08-29    0.003721           0.003728      0.003728         0.003728  \n",
       "\n",
       "[7445 rows x 10 columns]"
      ]
     },
     "execution_count": 42,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 比较不同方法计算结果的差异\n",
    "data_new"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "id": "592deb42",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 方法5：使用for循环计算收益率（不推荐，效率低）\n",
    "# 这种方法在大数据集上会非常慢，仅作为教学示例\n",
    "\n",
    "# 创建新列存储结果\n",
    "if 'Loop_return' not in data_new.columns:\n",
    "    data_new['Loop_return'] = np.nan\n",
    "\n",
    "# 使用for循环计算\n",
    "for i in range(len(data_new)):\n",
    "    data_new.iloc[i, data_new.columns.get_loc('Loop_return')] = data_new.iloc[i, data_new.columns.get_loc('Close')] / data_new.iloc[i, data_new.columns.get_loc('Preclose')] - 1\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "id": "b04f37ba",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 方法6：使用zip和enumerate组合（比纯for循环更Pythonic）\n",
    "close_values = data_new['Close'].values\n",
    "preclose_values = data_new['Preclose'].values\n",
    "loop_return_values = []\n",
    "\n",
    "for i, (close, preclose) in enumerate(zip(close_values, preclose_values)):\n",
    "    if preclose != 0 and not np.isnan(preclose):\n",
    "        loop_return_values.append(close / preclose - 1)\n",
    "    else:\n",
    "        loop_return_values.append(np.nan)\n",
    "\n",
    "data_new['Loop_return2'] = loop_return_values"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "id": "624be583",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 方法7：使用numpy的向量化操作（高效且简洁）\n",
    "data_new['Numpy_return'] = (data_new['Close'].values / data_new['Preclose'].values) - 1\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "id": "6738ab8e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.microsoft.datawrangler.viewer.v0+json": {
       "columns": [
        {
         "name": "Day",
         "rawType": "datetime64[ns]",
         "type": "datetime"
        },
        {
         "name": "Preclose",
         "rawType": "float64",
         "type": "float"
        },
        {
         "name": "Open",
         "rawType": "float64",
         "type": "float"
        },
        {
         "name": "Highest",
         "rawType": "float64",
         "type": "float"
        },
        {
         "name": "Lowest",
         "rawType": "float64",
         "type": "float"
        },
        {
         "name": "Close",
         "rawType": "float64",
         "type": "float"
        },
        {
         "name": "Raw_return",
         "rawType": "float64",
         "type": "float"
        },
        {
         "name": "Log_return",
         "rawType": "float64",
         "type": "float"
        },
        {
         "name": "Pct_change_return",
         "rawType": "float64",
         "type": "float"
        },
        {
         "name": "Apply_return",
         "rawType": "float64",
         "type": "float"
        },
        {
         "name": "Diff_div_return",
         "rawType": "float64",
         "type": "float"
        },
        {
         "name": "Loop_return",
         "rawType": "float64",
         "type": "float"
        },
        {
         "name": "Loop_return2",
         "rawType": "float64",
         "type": "float"
        },
        {
         "name": "Numpy_return",
         "rawType": "float64",
         "type": "float"
        }
       ],
       "ref": "bd951c04-be9c-40f0-8f6a-02bd5fe405e7",
       "rows": [
        [
         "1995-01-03 00:00:00",
         "647.87",
         "637.72",
         "647.71",
         "630.53",
         "639.88",
         "-0.01233272107058514",
         "-0.012409400168589713",
         null,
         "-0.01233272107058514",
         null,
         "-0.01233272107058514",
         "-0.01233272107058514",
         "-0.01233272107058514"
        ],
        [
         "1995-01-04 00:00:00",
         "639.88",
         "641.9",
         "655.51",
         "638.86",
         "653.81",
         "0.021769706820028656",
         "0.021536130597052505",
         "0.021769706820028656",
         "0.021769706820028656",
         "0.021769706820028677",
         "0.021769706820028656",
         "0.021769706820028656",
         "0.021769706820028656"
        ],
        [
         "1995-01-05 00:00:00",
         "653.81",
         "655.38",
         "657.52",
         "645.81",
         "646.89",
         "-0.010584114651045362",
         "-0.010640524779764071",
         "-0.010584114651045362",
         "-0.010584114651045362",
         "-0.010584114651045348",
         "-0.010584114651045362",
         "-0.010584114651045362",
         "-0.010584114651045362"
        ],
        [
         "1995-01-06 00:00:00",
         "646.89",
         "642.75",
         "643.89",
         "636.33",
         "640.76",
         "-0.00947610876656002",
         "-0.009521292757400879",
         "-0.00947610876656002",
         "-0.00947610876656002",
         "-0.009476108766559996",
         "-0.00947610876656002",
         "-0.00947610876656002",
         "-0.00947610876656002"
        ],
        [
         "1995-01-09 00:00:00",
         "640.76",
         "637.52",
         "637.55",
         "625.04",
         "626.0",
         "-0.023035145764404796",
         "-0.023304600733184522",
         "-0.023035145764404796",
         "-0.023035145764404796",
         "-0.023035145764404755",
         "-0.023035145764404796",
         "-0.023035145764404796",
         "-0.023035145764404796"
        ],
        [
         "1995-01-10 00:00:00",
         "626.0",
         "616.16",
         "617.6",
         "607.06",
         "610.3",
         "-0.02507987220447294",
         "-0.025399731549649474",
         "-0.02507987220447294",
         "-0.02507987220447294",
         "-0.025079872204472917",
         "-0.02507987220447294",
         "-0.02507987220447294",
         "-0.02507987220447294"
        ],
        [
         "1995-01-11 00:00:00",
         "610.3",
         "610.3",
         "615.21",
         "606.92",
         "613.13",
         "0.004637063739144853",
         "0.004626345679861643",
         "0.004637063739144853",
         "0.004637063739144853",
         "0.00463706373914475",
         "0.004637063739144853",
         "0.004637063739144853",
         "0.004637063739144853"
        ],
        [
         "1995-01-12 00:00:00",
         "613.13",
         "612.79",
         "615.47",
         "601.17",
         "602.12",
         "-0.017957040105687216",
         "-0.01812022423800741",
         "-0.017957040105687216",
         "-0.017957040105687216",
         "-0.017957040105687196",
         "-0.017957040105687216",
         "-0.017957040105687216",
         "-0.017957040105687216"
        ],
        [
         "1995-01-13 00:00:00",
         "602.12",
         "597.32",
         "601.07",
         "593.81",
         "597.84",
         "-0.007108217631036973",
         "-0.0071336013702687495",
         "-0.007108217631036973",
         "-0.007108217631036973",
         "-0.007108217631036957",
         "-0.007108217631036973",
         "-0.007108217631036973",
         "-0.007108217631036973"
        ],
        [
         "1995-01-16 00:00:00",
         "597.84",
         "591.06",
         "591.32",
         "584.88",
         "588.81",
         "-0.015104375752709953",
         "-0.0152196086559373",
         "-0.015104375752709953",
         "-0.015104375752709953",
         "-0.0151043757527099",
         "-0.015104375752709953",
         "-0.015104375752709953",
         "-0.015104375752709953"
        ],
        [
         "1995-01-17 00:00:00",
         "588.81",
         "591.81",
         "595.02",
         "586.6",
         "587.94",
         "-0.0014775564273702413",
         "-0.0014786490903144767",
         "-0.0014775564273702413",
         "-0.0014775564273702413",
         "-0.0014775564273702738",
         "-0.0014775564273702413",
         "-0.0014775564273702413",
         "-0.0014775564273702413"
        ],
        [
         "1995-01-18 00:00:00",
         "587.94",
         "588.53",
         "593.65",
         "586.99",
         "593.64",
         "0.009694866823145132",
         "0.009648173151834527",
         "0.009694866823145132",
         "0.009694866823145132",
         "0.009694866823145102",
         "0.009694866823145132",
         "0.009694866823145132",
         "0.009694866823145132"
        ],
        [
         "1995-01-19 00:00:00",
         "593.64",
         "589.7",
         "593.1",
         "571.36",
         "571.72",
         "-0.03692473552995079",
         "-0.03762371398670261",
         "-0.03692473552995079",
         "-0.03692473552995079",
         "-0.03692473552995074",
         "-0.03692473552995079",
         "-0.03692473552995079",
         "-0.03692473552995079"
        ],
        [
         "1995-01-20 00:00:00",
         "571.72",
         "566.81",
         "578.53",
         "559.66",
         "571.24",
         "-0.0008395718183726864",
         "-0.000839924456181862",
         "-0.0008395718183726864",
         "-0.0008395718183726864",
         "-0.0008395718183726617",
         "-0.0008395718183726864",
         "-0.0008395718183726864",
         "-0.0008395718183726864"
        ],
        [
         "1995-01-23 00:00:00",
         "571.24",
         "564.86",
         "565.04",
         "553.11",
         "553.44",
         "-0.031160282893354707",
         "-0.03165609139587389",
         "-0.031160282893354707",
         "-0.031160282893354707",
         "-0.031160282893354728",
         "-0.031160282893354707",
         "-0.031160282893354707",
         "-0.031160282893354707"
        ],
        [
         "1995-01-24 00:00:00",
         "553.44",
         "552.78",
         "559.34",
         "547.38",
         "547.57",
         "-0.01060638912980627",
         "-0.010663037789535146",
         "-0.01060638912980627",
         "-0.01060638912980627",
         "-0.010606389129806309",
         "-0.01060638912980627",
         "-0.01060638912980627",
         "-0.01060638912980627"
        ],
        [
         "1995-01-25 00:00:00",
         "547.57",
         "550.27",
         "558.58",
         "550.27",
         "555.77",
         "0.014975254305385377",
         "0.014864232201859728",
         "0.014975254305385377",
         "0.014975254305385377",
         "0.014975254305385486",
         "0.014975254305385377",
         "0.014975254305385377",
         "0.014975254305385377"
        ],
        [
         "1995-01-26 00:00:00",
         "555.77",
         "553.91",
         "555.26",
         "551.14",
         "553.11",
         "-0.004786152545117495",
         "-0.004797642850774331",
         "-0.004786152545117495",
         "-0.004786152545117495",
         "-0.004786152545117528",
         "-0.004786152545117495",
         "-0.004786152545117495",
         "-0.004786152545117495"
        ],
        [
         "1995-01-27 00:00:00",
         "553.11",
         "552.95",
         "564.61",
         "552.93",
         "562.59",
         "0.01713944784943333",
         "0.016994224529531365",
         "0.01713944784943333",
         "0.01713944784943333",
         "0.017139447849433238",
         "0.01713944784943333",
         "0.01713944784943333",
         "0.01713944784943333"
        ],
        [
         "1995-02-06 00:00:00",
         "562.59",
         "559.78",
         "559.84",
         "532.91",
         "533.72",
         "-0.05131623384702888",
         "-0.05267976440572042",
         "-0.05131623384702888",
         "-0.05131623384702888",
         "-0.05131623384702892",
         "-0.05131623384702888",
         "-0.05131623384702888",
         "-0.05131623384702888"
        ],
        [
         "1995-02-07 00:00:00",
         "533.72",
         "532.11",
         "536.63",
         "524.43",
         "532.49",
         "-0.0023045791800944126",
         "-0.0023072388096965213",
         "-0.0023045791800944126",
         "-0.0023045791800944126",
         "-0.0023045791800944656",
         "-0.0023045791800944126",
         "-0.0023045791800944126",
         "-0.0023045791800944126"
        ],
        [
         "1995-02-08 00:00:00",
         "532.49",
         "533.09",
         "537.55",
         "531.22",
         "533.75",
         "0.0023662416195608937",
         "0.002363446478311104",
         "0.0023662416195608937",
         "0.0023662416195608937",
         "0.0023662416195609137",
         "0.0023662416195608937",
         "0.0023662416195608937",
         "0.0023662416195608937"
        ],
        [
         "1995-02-09 00:00:00",
         "533.75",
         "533.79",
         "553.36",
         "533.14",
         "553.36",
         "0.03674004683840759",
         "0.03608121974949441",
         "0.03674004683840759",
         "0.03674004683840759",
         "0.03674004683840752",
         "0.03674004683840759",
         "0.03674004683840759",
         "0.03674004683840759"
        ],
        [
         "1995-02-10 00:00:00",
         "553.36",
         "548.77",
         "558.95",
         "540.63",
         "541.38",
         "-0.02164955905739485",
         "-0.021887349057864824",
         "-0.02164955905739485",
         "-0.02164955905739485",
         "-0.021649559057394855",
         "-0.02164955905739485",
         "-0.02164955905739485",
         "-0.02164955905739485"
        ],
        [
         "1995-02-13 00:00:00",
         "541.38",
         "537.95",
         "550.25",
         "537.05",
         "548.84",
         "0.01377960028076397",
         "0.013685524818510153",
         "0.01377960028076397",
         "0.01377960028076397",
         "0.013779600280764042",
         "0.01377960028076397",
         "0.01377960028076397",
         "0.01377960028076397"
        ],
        [
         "1995-02-14 00:00:00",
         "548.84",
         "549.76",
         "552.35",
         "542.18",
         "548.11",
         "-0.0013300779826543874",
         "-0.0013309633215072836",
         "-0.0013300779826543874",
         "-0.0013300779826543874",
         "-0.0013300779826543585",
         "-0.0013300779826543874",
         "-0.0013300779826543874",
         "-0.0013300779826543874"
        ],
        [
         "1995-02-15 00:00:00",
         "548.11",
         "547.83",
         "548.69",
         "542.5",
         "542.84",
         "-0.00961485833135678",
         "-0.009661379518297153",
         "-0.00961485833135678",
         "-0.00961485833135678",
         "-0.00961485833135681",
         "-0.00961485833135678",
         "-0.00961485833135678",
         "-0.00961485833135678"
        ],
        [
         "1995-02-16 00:00:00",
         "542.84",
         "542.26",
         "542.82",
         "537.21",
         "538.37",
         "-0.008234470562228369",
         "-0.008268561088893733",
         "-0.008234470562228369",
         "-0.008234470562228369",
         "-0.00823447056222833",
         "-0.008234470562228369",
         "-0.008234470562228369",
         "-0.008234470562228369"
        ],
        [
         "1995-02-17 00:00:00",
         "538.37",
         "537.87",
         "537.91",
         "532.67",
         "533.76",
         "-0.008562884261753112",
         "-0.00859975639394861",
         "-0.008562884261753112",
         "-0.008562884261753112",
         "-0.008562884261753095",
         "-0.008562884261753112",
         "-0.008562884261753112",
         "-0.008562884261753112"
        ],
        [
         "1995-02-20 00:00:00",
         "533.76",
         "534.57",
         "540.86",
         "534.51",
         "534.96",
         "0.0022482014388489624",
         "0.002245678015396102",
         "0.0022482014388489624",
         "0.0022482014388489624",
         "0.002248201438849006",
         "0.0022482014388489624",
         "0.0022482014388489624",
         "0.0022482014388489624"
        ],
        [
         "1995-02-21 00:00:00",
         "534.96",
         "536.07",
         "541.14",
         "535.97",
         "541.14",
         "0.011552265589950483",
         "0.01148604765994854",
         "0.011552265589950483",
         "0.011552265589950483",
         "0.011552265589950556",
         "0.011552265589950483",
         "0.011552265589950483",
         "0.011552265589950483"
        ],
        [
         "1995-02-22 00:00:00",
         "541.14",
         "541.21",
         "594.81",
         "541.21",
         "594.64",
         "0.09886535831762577",
         "0.0942781550113656",
         "0.09886535831762577",
         "0.09886535831762577",
         "0.09886535831762576",
         "0.09886535831762577",
         "0.09886535831762577",
         "0.09886535831762577"
        ],
        [
         "1995-02-23 00:00:00",
         "594.64",
         "589.9",
         "611.89",
         "575.82",
         "589.13",
         "-0.009266110587918708",
         "-0.00930930804599317",
         "-0.009266110587918708",
         "-0.009266110587918708",
         "-0.009266110587918725",
         "-0.009266110587918708",
         "-0.009266110587918708",
         "-0.009266110587918708"
        ],
        [
         "1995-02-24 00:00:00",
         "589.13",
         "591.91",
         "594.92",
         "555.2",
         "558.43",
         "-0.05211073956512158",
         "-0.05351759743764717",
         "-0.05211073956512158",
         "-0.05211073956512158",
         "-0.05211073956512153",
         "-0.05211073956512158",
         "-0.05211073956512158",
         "-0.05211073956512158"
        ],
        [
         "1995-02-27 00:00:00",
         "558.43",
         "584.02",
         "591.43",
         "559.04",
         "561.27",
         "0.005085686657235522",
         "0.005072798232039766",
         "0.005085686657235522",
         "0.005085686657235522",
         "0.005085686657235521",
         "0.005085686657235522",
         "0.005085686657235522",
         "0.005085686657235522"
        ],
        [
         "1995-02-28 00:00:00",
         "561.27",
         "558.42",
         "558.42",
         "544.06",
         "549.26",
         "-0.021397901188376367",
         "-0.02163015542103075",
         "-0.021397901188376367",
         "-0.021397901188376367",
         "-0.021397901188376346",
         "-0.021397901188376367",
         "-0.021397901188376367",
         "-0.021397901188376367"
        ],
        [
         "1995-03-01 00:00:00",
         "549.26",
         "548.91",
         "559.48",
         "545.72",
         "556.96",
         "0.014018861741251998",
         "0.013921506318722798",
         "0.014018861741251998",
         "0.014018861741251998",
         "0.01401886174125195",
         "0.014018861741251998",
         "0.014018861741251998",
         "0.014018861741251998"
        ],
        [
         "1995-03-02 00:00:00",
         "556.96",
         "557.5",
         "564.64",
         "553.52",
         "560.85",
         "0.006984343579431229",
         "0.0069600660281032845",
         "0.006984343579431229",
         "0.006984343579431229",
         "0.006984343579431173",
         "0.006984343579431229",
         "0.006984343579431229",
         "0.006984343579431229"
        ],
        [
         "1995-03-03 00:00:00",
         "560.85",
         "562.3",
         "580.39",
         "562.3",
         "577.22",
         "0.02918783988588758",
         "0.028769986242834555",
         "0.02918783988588758",
         "0.02918783988588758",
         "0.0291878398858875",
         "0.02918783988588758",
         "0.02918783988588758",
         "0.02918783988588758"
        ],
        [
         "1995-03-06 00:00:00",
         "577.22",
         "585.24",
         "616.76",
         "582.55",
         "616.3",
         "0.06770382176639744",
         "0.06551038163413025",
         "0.06770382176639744",
         "0.06770382176639744",
         "0.06770382176639743",
         "0.06770382176639744",
         "0.06770382176639744",
         "0.06770382176639744"
        ],
        [
         "1995-03-07 00:00:00",
         "616.3",
         "614.73",
         "635.24",
         "597.2",
         "632.96",
         "0.027032289469414383",
         "0.026673371026095793",
         "0.027032289469414383",
         "0.027032289469414383",
         "0.02703228946941438",
         "0.027032289469414383",
         "0.027032289469414383",
         "0.027032289469414383"
        ],
        [
         "1995-03-08 00:00:00",
         "632.96",
         "632.96",
         "634.2",
         "618.8",
         "620.81",
         "-0.019195525783619938",
         "-0.019382152007597853",
         "-0.019195525783619938",
         "-0.019195525783619938",
         "-0.019195525783619962",
         "-0.019195525783619938",
         "-0.019195525783619938",
         "-0.019195525783619938"
        ],
        [
         "1995-03-09 00:00:00",
         "620.81",
         "617.22",
         "624.25",
         "604.79",
         "605.59",
         "-0.0245163576617643",
         "-0.02482188754932757",
         "-0.0245163576617643",
         "-0.0245163576617643",
         "-0.024516357661764333",
         "-0.0245163576617643",
         "-0.0245163576617643",
         "-0.0245163576617643"
        ],
        [
         "1995-03-10 00:00:00",
         "605.59",
         "600.45",
         "600.51",
         "583.72",
         "586.29",
         "-0.03186974685843569",
         "-0.032388641727272294",
         "-0.03186974685843569",
         "-0.03186974685843569",
         "-0.03186974685843569",
         "-0.03186974685843569",
         "-0.03186974685843569",
         "-0.03186974685843569"
        ],
        [
         "1995-03-13 00:00:00",
         "586.29",
         "596.65",
         "597.18",
         "583.38",
         "586.23",
         "-0.00010233843319851221",
         "-0.00010234367013328693",
         "-0.00010233843319851221",
         "-0.00010233843319851221",
         "-0.00010233843319849466",
         "-0.00010233843319851221",
         "-0.00010233843319851221",
         "-0.00010233843319851221"
        ],
        [
         "1995-03-14 00:00:00",
         "586.23",
         "588.18",
         "599.9",
         "586.19",
         "588.24",
         "0.003428688398751323",
         "0.0034228238480045192",
         "0.003428688398751323",
         "0.003428688398751323",
         "0.0034286883987513276",
         "0.003428688398751323",
         "0.003428688398751323",
         "0.003428688398751323"
        ],
        [
         "1995-03-15 00:00:00",
         "588.24",
         "588.78",
         "591.49",
         "568.6",
         "572.6",
         "-0.026587787297701615",
         "-0.026947635223951494",
         "-0.026587787297701615",
         "-0.026587787297701615",
         "-0.026587787297701594",
         "-0.026587787297701615",
         "-0.026587787297701615",
         "-0.026587787297701615"
        ],
        [
         "1995-03-16 00:00:00",
         "572.6",
         "570.51",
         "579.88",
         "569.17",
         "579.88",
         "0.012713936430317707",
         "0.012633792918681941",
         "0.012713936430317707",
         "0.012713936430317707",
         "0.0127139364303178",
         "0.012713936430317707",
         "0.012713936430317707",
         "0.012713936430317707"
        ],
        [
         "1995-03-17 00:00:00",
         "579.88",
         "579.62",
         "593.97",
         "577.91",
         "593.52",
         "0.02352210802234933",
         "0.023249726298573137",
         "0.02352210802234933",
         "0.02352210802234933",
         "0.023522108022349428",
         "0.02352210802234933",
         "0.02352210802234933",
         "0.02352210802234933"
        ],
        [
         "1995-03-20 00:00:00",
         "593.52",
         "597.85",
         "604.96",
         "585.15",
         "585.45",
         "-0.01359684593610988",
         "-0.013690129586536592",
         "-0.01359684593610988",
         "-0.01359684593610988",
         "-0.01359684593610988",
         "-0.01359684593610988",
         "-0.01359684593610988",
         "-0.01359684593610988"
        ]
       ],
       "shape": {
        "columns": 13,
        "rows": 7445
       }
      },
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Preclose</th>\n",
       "      <th>Open</th>\n",
       "      <th>Highest</th>\n",
       "      <th>Lowest</th>\n",
       "      <th>Close</th>\n",
       "      <th>Raw_return</th>\n",
       "      <th>Log_return</th>\n",
       "      <th>Pct_change_return</th>\n",
       "      <th>Apply_return</th>\n",
       "      <th>Diff_div_return</th>\n",
       "      <th>Loop_return</th>\n",
       "      <th>Loop_return2</th>\n",
       "      <th>Numpy_return</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Day</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>1995-01-03</th>\n",
       "      <td>647.870</td>\n",
       "      <td>637.720</td>\n",
       "      <td>647.710</td>\n",
       "      <td>630.530</td>\n",
       "      <td>639.880</td>\n",
       "      <td>-0.012333</td>\n",
       "      <td>-0.012409</td>\n",
       "      <td>NaN</td>\n",
       "      <td>-0.012333</td>\n",
       "      <td>NaN</td>\n",
       "      <td>-0.012333</td>\n",
       "      <td>-0.012333</td>\n",
       "      <td>-0.012333</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1995-01-04</th>\n",
       "      <td>639.880</td>\n",
       "      <td>641.900</td>\n",
       "      <td>655.510</td>\n",
       "      <td>638.860</td>\n",
       "      <td>653.810</td>\n",
       "      <td>0.021770</td>\n",
       "      <td>0.021536</td>\n",
       "      <td>0.021770</td>\n",
       "      <td>0.021770</td>\n",
       "      <td>0.021770</td>\n",
       "      <td>0.021770</td>\n",
       "      <td>0.021770</td>\n",
       "      <td>0.021770</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1995-01-05</th>\n",
       "      <td>653.810</td>\n",
       "      <td>655.380</td>\n",
       "      <td>657.520</td>\n",
       "      <td>645.810</td>\n",
       "      <td>646.890</td>\n",
       "      <td>-0.010584</td>\n",
       "      <td>-0.010641</td>\n",
       "      <td>-0.010584</td>\n",
       "      <td>-0.010584</td>\n",
       "      <td>-0.010584</td>\n",
       "      <td>-0.010584</td>\n",
       "      <td>-0.010584</td>\n",
       "      <td>-0.010584</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1995-01-06</th>\n",
       "      <td>646.890</td>\n",
       "      <td>642.750</td>\n",
       "      <td>643.890</td>\n",
       "      <td>636.330</td>\n",
       "      <td>640.760</td>\n",
       "      <td>-0.009476</td>\n",
       "      <td>-0.009521</td>\n",
       "      <td>-0.009476</td>\n",
       "      <td>-0.009476</td>\n",
       "      <td>-0.009476</td>\n",
       "      <td>-0.009476</td>\n",
       "      <td>-0.009476</td>\n",
       "      <td>-0.009476</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1995-01-09</th>\n",
       "      <td>640.760</td>\n",
       "      <td>637.520</td>\n",
       "      <td>637.550</td>\n",
       "      <td>625.040</td>\n",
       "      <td>626.000</td>\n",
       "      <td>-0.023035</td>\n",
       "      <td>-0.023305</td>\n",
       "      <td>-0.023035</td>\n",
       "      <td>-0.023035</td>\n",
       "      <td>-0.023035</td>\n",
       "      <td>-0.023035</td>\n",
       "      <td>-0.023035</td>\n",
       "      <td>-0.023035</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2025-08-25</th>\n",
       "      <td>3825.759</td>\n",
       "      <td>3848.163</td>\n",
       "      <td>3883.562</td>\n",
       "      <td>3839.972</td>\n",
       "      <td>3883.562</td>\n",
       "      <td>0.015109</td>\n",
       "      <td>0.014996</td>\n",
       "      <td>0.015109</td>\n",
       "      <td>0.015109</td>\n",
       "      <td>0.015109</td>\n",
       "      <td>0.015109</td>\n",
       "      <td>0.015109</td>\n",
       "      <td>0.015109</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2025-08-26</th>\n",
       "      <td>3883.562</td>\n",
       "      <td>3871.471</td>\n",
       "      <td>3888.599</td>\n",
       "      <td>3859.758</td>\n",
       "      <td>3868.382</td>\n",
       "      <td>-0.003909</td>\n",
       "      <td>-0.003916</td>\n",
       "      <td>-0.003909</td>\n",
       "      <td>-0.003909</td>\n",
       "      <td>-0.003909</td>\n",
       "      <td>-0.003909</td>\n",
       "      <td>-0.003909</td>\n",
       "      <td>-0.003909</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2025-08-27</th>\n",
       "      <td>3868.382</td>\n",
       "      <td>3869.612</td>\n",
       "      <td>3887.198</td>\n",
       "      <td>3800.350</td>\n",
       "      <td>3800.350</td>\n",
       "      <td>-0.017587</td>\n",
       "      <td>-0.017743</td>\n",
       "      <td>-0.017587</td>\n",
       "      <td>-0.017587</td>\n",
       "      <td>-0.017587</td>\n",
       "      <td>-0.017587</td>\n",
       "      <td>-0.017587</td>\n",
       "      <td>-0.017587</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2025-08-28</th>\n",
       "      <td>3800.350</td>\n",
       "      <td>3796.711</td>\n",
       "      <td>3845.087</td>\n",
       "      <td>3761.422</td>\n",
       "      <td>3843.597</td>\n",
       "      <td>0.011380</td>\n",
       "      <td>0.011315</td>\n",
       "      <td>0.011380</td>\n",
       "      <td>0.011380</td>\n",
       "      <td>0.011380</td>\n",
       "      <td>0.011380</td>\n",
       "      <td>0.011380</td>\n",
       "      <td>0.011380</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2025-08-29</th>\n",
       "      <td>3843.597</td>\n",
       "      <td>3842.823</td>\n",
       "      <td>3867.606</td>\n",
       "      <td>3839.206</td>\n",
       "      <td>3857.927</td>\n",
       "      <td>0.003728</td>\n",
       "      <td>0.003721</td>\n",
       "      <td>0.003728</td>\n",
       "      <td>0.003728</td>\n",
       "      <td>0.003728</td>\n",
       "      <td>0.003728</td>\n",
       "      <td>0.003728</td>\n",
       "      <td>0.003728</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>7445 rows × 13 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "            Preclose      Open   Highest    Lowest     Close  Raw_return  \\\n",
       "Day                                                                        \n",
       "1995-01-03   647.870   637.720   647.710   630.530   639.880   -0.012333   \n",
       "1995-01-04   639.880   641.900   655.510   638.860   653.810    0.021770   \n",
       "1995-01-05   653.810   655.380   657.520   645.810   646.890   -0.010584   \n",
       "1995-01-06   646.890   642.750   643.890   636.330   640.760   -0.009476   \n",
       "1995-01-09   640.760   637.520   637.550   625.040   626.000   -0.023035   \n",
       "...              ...       ...       ...       ...       ...         ...   \n",
       "2025-08-25  3825.759  3848.163  3883.562  3839.972  3883.562    0.015109   \n",
       "2025-08-26  3883.562  3871.471  3888.599  3859.758  3868.382   -0.003909   \n",
       "2025-08-27  3868.382  3869.612  3887.198  3800.350  3800.350   -0.017587   \n",
       "2025-08-28  3800.350  3796.711  3845.087  3761.422  3843.597    0.011380   \n",
       "2025-08-29  3843.597  3842.823  3867.606  3839.206  3857.927    0.003728   \n",
       "\n",
       "            Log_return  Pct_change_return  Apply_return  Diff_div_return  \\\n",
       "Day                                                                        \n",
       "1995-01-03   -0.012409                NaN     -0.012333              NaN   \n",
       "1995-01-04    0.021536           0.021770      0.021770         0.021770   \n",
       "1995-01-05   -0.010641          -0.010584     -0.010584        -0.010584   \n",
       "1995-01-06   -0.009521          -0.009476     -0.009476        -0.009476   \n",
       "1995-01-09   -0.023305          -0.023035     -0.023035        -0.023035   \n",
       "...                ...                ...           ...              ...   \n",
       "2025-08-25    0.014996           0.015109      0.015109         0.015109   \n",
       "2025-08-26   -0.003916          -0.003909     -0.003909        -0.003909   \n",
       "2025-08-27   -0.017743          -0.017587     -0.017587        -0.017587   \n",
       "2025-08-28    0.011315           0.011380      0.011380         0.011380   \n",
       "2025-08-29    0.003721           0.003728      0.003728         0.003728   \n",
       "\n",
       "            Loop_return  Loop_return2  Numpy_return  \n",
       "Day                                                  \n",
       "1995-01-03    -0.012333     -0.012333     -0.012333  \n",
       "1995-01-04     0.021770      0.021770      0.021770  \n",
       "1995-01-05    -0.010584     -0.010584     -0.010584  \n",
       "1995-01-06    -0.009476     -0.009476     -0.009476  \n",
       "1995-01-09    -0.023035     -0.023035     -0.023035  \n",
       "...                 ...           ...           ...  \n",
       "2025-08-25     0.015109      0.015109      0.015109  \n",
       "2025-08-26    -0.003909     -0.003909     -0.003909  \n",
       "2025-08-27    -0.017587     -0.017587     -0.017587  \n",
       "2025-08-28     0.011380      0.011380      0.011380  \n",
       "2025-08-29     0.003728      0.003728      0.003728  \n",
       "\n",
       "[7445 rows x 13 columns]"
      ]
     },
     "execution_count": 46,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 显示结果\n",
    "data_new"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "id": "79d0d422",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.microsoft.datawrangler.viewer.v0+json": {
       "columns": [
        {
         "name": "Day",
         "rawType": "datetime64[ns]",
         "type": "datetime"
        },
        {
         "name": "Log_return",
         "rawType": "float64",
         "type": "float"
        },
        {
         "name": "Raw_Return",
         "rawType": "float64",
         "type": "float"
        },
        {
         "name": "Year",
         "rawType": "int32",
         "type": "integer"
        },
        {
         "name": "Month",
         "rawType": "int32",
         "type": "integer"
        }
       ],
       "ref": "c6d38d24-00c3-4cd6-8eba-aff21fbe41fc",
       "rows": [
        [
         "1995-01-31 00:00:00",
         "-0.14113893766204466",
         "-0.13163134579468116",
         "1995",
         "1"
        ],
        [
         "1995-02-28 00:00:00",
         "-0.023979203535533955",
         "-0.023693986739899198",
         "1995",
         "2"
        ],
        [
         "1995-03-31 00:00:00",
         "0.16365072150717097",
         "0.17780286203255224",
         "1995",
         "3"
        ],
        [
         "1995-04-30 00:00:00",
         "-0.10931523264986698",
         "-0.10355221665739145",
         "1995",
         "4"
        ],
        [
         "1995-05-31 00:00:00",
         "0.18890123459094443",
         "0.2079216457158617",
         "1995",
         "5"
        ]
       ],
       "shape": {
        "columns": 4,
        "rows": 5
       }
      },
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Log_return</th>\n",
       "      <th>Raw_Return</th>\n",
       "      <th>Year</th>\n",
       "      <th>Month</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Day</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>1995-01-31</th>\n",
       "      <td>-0.141139</td>\n",
       "      <td>-0.131631</td>\n",
       "      <td>1995</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1995-02-28</th>\n",
       "      <td>-0.023979</td>\n",
       "      <td>-0.023694</td>\n",
       "      <td>1995</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1995-03-31</th>\n",
       "      <td>0.163651</td>\n",
       "      <td>0.177803</td>\n",
       "      <td>1995</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1995-04-30</th>\n",
       "      <td>-0.109315</td>\n",
       "      <td>-0.103552</td>\n",
       "      <td>1995</td>\n",
       "      <td>4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1995-05-31</th>\n",
       "      <td>0.188901</td>\n",
       "      <td>0.207922</td>\n",
       "      <td>1995</td>\n",
       "      <td>5</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "            Log_return  Raw_Return  Year  Month\n",
       "Day                                            \n",
       "1995-01-31   -0.141139   -0.131631  1995      1\n",
       "1995-02-28   -0.023979   -0.023694  1995      2\n",
       "1995-03-31    0.163651    0.177803  1995      3\n",
       "1995-04-30   -0.109315   -0.103552  1995      4\n",
       "1995-05-31    0.188901    0.207922  1995      5"
      ]
     },
     "execution_count": 47,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 方法1：使用resample函数计算月度对数收益率并转换为原始收益率\n",
    "# 这种方法适合对数收益率，因为对数收益率可以直接相加\n",
    "Month_data1 = data_new.resample('ME')['Log_return'].sum().to_frame(name='Log_return') \n",
    "Month_data1['Raw_Return'] = np.exp(Month_data1['Log_return']) - 1\n",
    "\n",
    "# 添加年月信息便于分析\n",
    "Month_data1['Year'] = Month_data1.index.year\n",
    "Month_data1['Month'] = Month_data1.index.month\n",
    "\n",
    "# 显示结果\n",
    "Month_data1.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "id": "bb16ee8e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.microsoft.datawrangler.viewer.v0+json": {
       "columns": [
        {
         "name": "Day",
         "rawType": "datetime64[ns]",
         "type": "datetime"
        },
        {
         "name": "Close",
         "rawType": "float64",
         "type": "float"
        },
        {
         "name": "Preclose",
         "rawType": "float64",
         "type": "float"
        },
        {
         "name": "Raw_return",
         "rawType": "float64",
         "type": "float"
        },
        {
         "name": "Log_return",
         "rawType": "float64",
         "type": "float"
        },
        {
         "name": "Year",
         "rawType": "int32",
         "type": "integer"
        },
        {
         "name": "Month",
         "rawType": "int32",
         "type": "integer"
        }
       ],
       "ref": "8a3d5675-89e1-4b72-bc44-948d2aa0c5c1",
       "rows": [
        [
         "1995-01-31 00:00:00",
         "562.59",
         null,
         null,
         null,
         "1995",
         "1"
        ],
        [
         "1995-02-28 00:00:00",
         "549.26",
         "562.59",
         "-0.02369398673989942",
         "-0.023979203535533955",
         "1995",
         "2"
        ],
        [
         "1995-03-31 00:00:00",
         "646.92",
         "549.26",
         "0.1778028620325529",
         "0.16365072150717097",
         "1995",
         "3"
        ],
        [
         "1995-04-30 00:00:00",
         "579.93",
         "646.92",
         "-0.103552216657392",
         "-0.10931523264986698",
         "1995",
         "4"
        ],
        [
         "1995-05-31 00:00:00",
         "700.51",
         "579.93",
         "0.20792164571586236",
         "0.18890123459094443",
         "1995",
         "5"
        ]
       ],
       "shape": {
        "columns": 6,
        "rows": 5
       }
      },
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Close</th>\n",
       "      <th>Preclose</th>\n",
       "      <th>Raw_return</th>\n",
       "      <th>Log_return</th>\n",
       "      <th>Year</th>\n",
       "      <th>Month</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Day</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>1995-01-31</th>\n",
       "      <td>562.59</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>1995</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1995-02-28</th>\n",
       "      <td>549.26</td>\n",
       "      <td>562.59</td>\n",
       "      <td>-0.023694</td>\n",
       "      <td>-0.023979</td>\n",
       "      <td>1995</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1995-03-31</th>\n",
       "      <td>646.92</td>\n",
       "      <td>549.26</td>\n",
       "      <td>0.177803</td>\n",
       "      <td>0.163651</td>\n",
       "      <td>1995</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1995-04-30</th>\n",
       "      <td>579.93</td>\n",
       "      <td>646.92</td>\n",
       "      <td>-0.103552</td>\n",
       "      <td>-0.109315</td>\n",
       "      <td>1995</td>\n",
       "      <td>4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1995-05-31</th>\n",
       "      <td>700.51</td>\n",
       "      <td>579.93</td>\n",
       "      <td>0.207922</td>\n",
       "      <td>0.188901</td>\n",
       "      <td>1995</td>\n",
       "      <td>5</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "             Close  Preclose  Raw_return  Log_return  Year  Month\n",
       "Day                                                              \n",
       "1995-01-31  562.59       NaN         NaN         NaN  1995      1\n",
       "1995-02-28  549.26    562.59   -0.023694   -0.023979  1995      2\n",
       "1995-03-31  646.92    549.26    0.177803    0.163651  1995      3\n",
       "1995-04-30  579.93    646.92   -0.103552   -0.109315  1995      4\n",
       "1995-05-31  700.51    579.93    0.207922    0.188901  1995      5"
      ]
     },
     "execution_count": 48,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 方法2：使用resample取月末价格计算月度收益率\n",
    "# 这种方法直接使用月末价格计算收益率，更符合金融实践\n",
    "Month_data2 = data_new.resample('ME')['Close'].last().to_frame()\n",
    "Month_data2['Preclose'] = Month_data2['Close'].shift(1)\n",
    "Month_data2['Raw_return'] = Month_data2['Close'] / Month_data2['Preclose'] - 1\n",
    "Month_data2['Log_return'] = np.log(Month_data2['Close']) - np.log(Month_data2['Preclose'])\n",
    "\n",
    "# 添加年月信息\n",
    "Month_data2['Year'] = Month_data2.index.year\n",
    "Month_data2['Month'] = Month_data2.index.month\n",
    "\n",
    "# 显示结果\n",
    "Month_data2.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "id": "041e87dd",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.microsoft.datawrangler.viewer.v0+json": {
       "columns": [
        {
         "name": "Day",
         "rawType": "datetime64[ns]",
         "type": "datetime"
        },
        {
         "name": "Preclose",
         "rawType": "float64",
         "type": "float"
        },
        {
         "name": "Open",
         "rawType": "float64",
         "type": "float"
        },
        {
         "name": "Highest",
         "rawType": "float64",
         "type": "float"
        },
        {
         "name": "Lowest",
         "rawType": "float64",
         "type": "float"
        },
        {
         "name": "Close",
         "rawType": "float64",
         "type": "float"
        },
        {
         "name": "Raw_return",
         "rawType": "float64",
         "type": "float"
        },
        {
         "name": "Log_return",
         "rawType": "float64",
         "type": "float"
        },
        {
         "name": "Pct_change_return",
         "rawType": "float64",
         "type": "float"
        },
        {
         "name": "Apply_return",
         "rawType": "float64",
         "type": "float"
        },
        {
         "name": "Diff_div_return",
         "rawType": "float64",
         "type": "float"
        },
        {
         "name": "Loop_return",
         "rawType": "float64",
         "type": "float"
        },
        {
         "name": "Loop_return2",
         "rawType": "float64",
         "type": "float"
        },
        {
         "name": "Numpy_return",
         "rawType": "float64",
         "type": "float"
        },
        {
         "name": "year",
         "rawType": "int32",
         "type": "integer"
        },
        {
         "name": "month",
         "rawType": "int32",
         "type": "integer"
        }
       ],
       "ref": "6ff1d4e0-f277-4070-8a82-71119a52526c",
       "rows": [
        [
         "1995-01-03 00:00:00",
         "647.87",
         "637.72",
         "647.71",
         "630.53",
         "639.88",
         "-0.01233272107058514",
         "-0.012409400168589713",
         null,
         "-0.01233272107058514",
         null,
         "-0.01233272107058514",
         "-0.01233272107058514",
         "-0.01233272107058514",
         "1995",
         "1"
        ],
        [
         "1995-01-04 00:00:00",
         "639.88",
         "641.9",
         "655.51",
         "638.86",
         "653.81",
         "0.021769706820028656",
         "0.021536130597052505",
         "0.021769706820028656",
         "0.021769706820028656",
         "0.021769706820028677",
         "0.021769706820028656",
         "0.021769706820028656",
         "0.021769706820028656",
         "1995",
         "1"
        ],
        [
         "1995-01-05 00:00:00",
         "653.81",
         "655.38",
         "657.52",
         "645.81",
         "646.89",
         "-0.010584114651045362",
         "-0.010640524779764071",
         "-0.010584114651045362",
         "-0.010584114651045362",
         "-0.010584114651045348",
         "-0.010584114651045362",
         "-0.010584114651045362",
         "-0.010584114651045362",
         "1995",
         "1"
        ],
        [
         "1995-01-06 00:00:00",
         "646.89",
         "642.75",
         "643.89",
         "636.33",
         "640.76",
         "-0.00947610876656002",
         "-0.009521292757400879",
         "-0.00947610876656002",
         "-0.00947610876656002",
         "-0.009476108766559996",
         "-0.00947610876656002",
         "-0.00947610876656002",
         "-0.00947610876656002",
         "1995",
         "1"
        ],
        [
         "1995-01-09 00:00:00",
         "640.76",
         "637.52",
         "637.55",
         "625.04",
         "626.0",
         "-0.023035145764404796",
         "-0.023304600733184522",
         "-0.023035145764404796",
         "-0.023035145764404796",
         "-0.023035145764404755",
         "-0.023035145764404796",
         "-0.023035145764404796",
         "-0.023035145764404796",
         "1995",
         "1"
        ],
        [
         "1995-01-10 00:00:00",
         "626.0",
         "616.16",
         "617.6",
         "607.06",
         "610.3",
         "-0.02507987220447294",
         "-0.025399731549649474",
         "-0.02507987220447294",
         "-0.02507987220447294",
         "-0.025079872204472917",
         "-0.02507987220447294",
         "-0.02507987220447294",
         "-0.02507987220447294",
         "1995",
         "1"
        ],
        [
         "1995-01-11 00:00:00",
         "610.3",
         "610.3",
         "615.21",
         "606.92",
         "613.13",
         "0.004637063739144853",
         "0.004626345679861643",
         "0.004637063739144853",
         "0.004637063739144853",
         "0.00463706373914475",
         "0.004637063739144853",
         "0.004637063739144853",
         "0.004637063739144853",
         "1995",
         "1"
        ],
        [
         "1995-01-12 00:00:00",
         "613.13",
         "612.79",
         "615.47",
         "601.17",
         "602.12",
         "-0.017957040105687216",
         "-0.01812022423800741",
         "-0.017957040105687216",
         "-0.017957040105687216",
         "-0.017957040105687196",
         "-0.017957040105687216",
         "-0.017957040105687216",
         "-0.017957040105687216",
         "1995",
         "1"
        ],
        [
         "1995-01-13 00:00:00",
         "602.12",
         "597.32",
         "601.07",
         "593.81",
         "597.84",
         "-0.007108217631036973",
         "-0.0071336013702687495",
         "-0.007108217631036973",
         "-0.007108217631036973",
         "-0.007108217631036957",
         "-0.007108217631036973",
         "-0.007108217631036973",
         "-0.007108217631036973",
         "1995",
         "1"
        ],
        [
         "1995-01-16 00:00:00",
         "597.84",
         "591.06",
         "591.32",
         "584.88",
         "588.81",
         "-0.015104375752709953",
         "-0.0152196086559373",
         "-0.015104375752709953",
         "-0.015104375752709953",
         "-0.0151043757527099",
         "-0.015104375752709953",
         "-0.015104375752709953",
         "-0.015104375752709953",
         "1995",
         "1"
        ],
        [
         "1995-01-17 00:00:00",
         "588.81",
         "591.81",
         "595.02",
         "586.6",
         "587.94",
         "-0.0014775564273702413",
         "-0.0014786490903144767",
         "-0.0014775564273702413",
         "-0.0014775564273702413",
         "-0.0014775564273702738",
         "-0.0014775564273702413",
         "-0.0014775564273702413",
         "-0.0014775564273702413",
         "1995",
         "1"
        ],
        [
         "1995-01-18 00:00:00",
         "587.94",
         "588.53",
         "593.65",
         "586.99",
         "593.64",
         "0.009694866823145132",
         "0.009648173151834527",
         "0.009694866823145132",
         "0.009694866823145132",
         "0.009694866823145102",
         "0.009694866823145132",
         "0.009694866823145132",
         "0.009694866823145132",
         "1995",
         "1"
        ],
        [
         "1995-01-19 00:00:00",
         "593.64",
         "589.7",
         "593.1",
         "571.36",
         "571.72",
         "-0.03692473552995079",
         "-0.03762371398670261",
         "-0.03692473552995079",
         "-0.03692473552995079",
         "-0.03692473552995074",
         "-0.03692473552995079",
         "-0.03692473552995079",
         "-0.03692473552995079",
         "1995",
         "1"
        ],
        [
         "1995-01-20 00:00:00",
         "571.72",
         "566.81",
         "578.53",
         "559.66",
         "571.24",
         "-0.0008395718183726864",
         "-0.000839924456181862",
         "-0.0008395718183726864",
         "-0.0008395718183726864",
         "-0.0008395718183726617",
         "-0.0008395718183726864",
         "-0.0008395718183726864",
         "-0.0008395718183726864",
         "1995",
         "1"
        ],
        [
         "1995-01-23 00:00:00",
         "571.24",
         "564.86",
         "565.04",
         "553.11",
         "553.44",
         "-0.031160282893354707",
         "-0.03165609139587389",
         "-0.031160282893354707",
         "-0.031160282893354707",
         "-0.031160282893354728",
         "-0.031160282893354707",
         "-0.031160282893354707",
         "-0.031160282893354707",
         "1995",
         "1"
        ],
        [
         "1995-01-24 00:00:00",
         "553.44",
         "552.78",
         "559.34",
         "547.38",
         "547.57",
         "-0.01060638912980627",
         "-0.010663037789535146",
         "-0.01060638912980627",
         "-0.01060638912980627",
         "-0.010606389129806309",
         "-0.01060638912980627",
         "-0.01060638912980627",
         "-0.01060638912980627",
         "1995",
         "1"
        ],
        [
         "1995-01-25 00:00:00",
         "547.57",
         "550.27",
         "558.58",
         "550.27",
         "555.77",
         "0.014975254305385377",
         "0.014864232201859728",
         "0.014975254305385377",
         "0.014975254305385377",
         "0.014975254305385486",
         "0.014975254305385377",
         "0.014975254305385377",
         "0.014975254305385377",
         "1995",
         "1"
        ],
        [
         "1995-01-26 00:00:00",
         "555.77",
         "553.91",
         "555.26",
         "551.14",
         "553.11",
         "-0.004786152545117495",
         "-0.004797642850774331",
         "-0.004786152545117495",
         "-0.004786152545117495",
         "-0.004786152545117528",
         "-0.004786152545117495",
         "-0.004786152545117495",
         "-0.004786152545117495",
         "1995",
         "1"
        ],
        [
         "1995-01-27 00:00:00",
         "553.11",
         "552.95",
         "564.61",
         "552.93",
         "562.59",
         "0.01713944784943333",
         "0.016994224529531365",
         "0.01713944784943333",
         "0.01713944784943333",
         "0.017139447849433238",
         "0.01713944784943333",
         "0.01713944784943333",
         "0.01713944784943333",
         "1995",
         "1"
        ],
        [
         "1995-02-06 00:00:00",
         "562.59",
         "559.78",
         "559.84",
         "532.91",
         "533.72",
         "-0.05131623384702888",
         "-0.05267976440572042",
         "-0.05131623384702888",
         "-0.05131623384702888",
         "-0.05131623384702892",
         "-0.05131623384702888",
         "-0.05131623384702888",
         "-0.05131623384702888",
         "1995",
         "2"
        ],
        [
         "1995-02-07 00:00:00",
         "533.72",
         "532.11",
         "536.63",
         "524.43",
         "532.49",
         "-0.0023045791800944126",
         "-0.0023072388096965213",
         "-0.0023045791800944126",
         "-0.0023045791800944126",
         "-0.0023045791800944656",
         "-0.0023045791800944126",
         "-0.0023045791800944126",
         "-0.0023045791800944126",
         "1995",
         "2"
        ],
        [
         "1995-02-08 00:00:00",
         "532.49",
         "533.09",
         "537.55",
         "531.22",
         "533.75",
         "0.0023662416195608937",
         "0.002363446478311104",
         "0.0023662416195608937",
         "0.0023662416195608937",
         "0.0023662416195609137",
         "0.0023662416195608937",
         "0.0023662416195608937",
         "0.0023662416195608937",
         "1995",
         "2"
        ],
        [
         "1995-02-09 00:00:00",
         "533.75",
         "533.79",
         "553.36",
         "533.14",
         "553.36",
         "0.03674004683840759",
         "0.03608121974949441",
         "0.03674004683840759",
         "0.03674004683840759",
         "0.03674004683840752",
         "0.03674004683840759",
         "0.03674004683840759",
         "0.03674004683840759",
         "1995",
         "2"
        ],
        [
         "1995-02-10 00:00:00",
         "553.36",
         "548.77",
         "558.95",
         "540.63",
         "541.38",
         "-0.02164955905739485",
         "-0.021887349057864824",
         "-0.02164955905739485",
         "-0.02164955905739485",
         "-0.021649559057394855",
         "-0.02164955905739485",
         "-0.02164955905739485",
         "-0.02164955905739485",
         "1995",
         "2"
        ],
        [
         "1995-02-13 00:00:00",
         "541.38",
         "537.95",
         "550.25",
         "537.05",
         "548.84",
         "0.01377960028076397",
         "0.013685524818510153",
         "0.01377960028076397",
         "0.01377960028076397",
         "0.013779600280764042",
         "0.01377960028076397",
         "0.01377960028076397",
         "0.01377960028076397",
         "1995",
         "2"
        ],
        [
         "1995-02-14 00:00:00",
         "548.84",
         "549.76",
         "552.35",
         "542.18",
         "548.11",
         "-0.0013300779826543874",
         "-0.0013309633215072836",
         "-0.0013300779826543874",
         "-0.0013300779826543874",
         "-0.0013300779826543585",
         "-0.0013300779826543874",
         "-0.0013300779826543874",
         "-0.0013300779826543874",
         "1995",
         "2"
        ],
        [
         "1995-02-15 00:00:00",
         "548.11",
         "547.83",
         "548.69",
         "542.5",
         "542.84",
         "-0.00961485833135678",
         "-0.009661379518297153",
         "-0.00961485833135678",
         "-0.00961485833135678",
         "-0.00961485833135681",
         "-0.00961485833135678",
         "-0.00961485833135678",
         "-0.00961485833135678",
         "1995",
         "2"
        ],
        [
         "1995-02-16 00:00:00",
         "542.84",
         "542.26",
         "542.82",
         "537.21",
         "538.37",
         "-0.008234470562228369",
         "-0.008268561088893733",
         "-0.008234470562228369",
         "-0.008234470562228369",
         "-0.00823447056222833",
         "-0.008234470562228369",
         "-0.008234470562228369",
         "-0.008234470562228369",
         "1995",
         "2"
        ],
        [
         "1995-02-17 00:00:00",
         "538.37",
         "537.87",
         "537.91",
         "532.67",
         "533.76",
         "-0.008562884261753112",
         "-0.00859975639394861",
         "-0.008562884261753112",
         "-0.008562884261753112",
         "-0.008562884261753095",
         "-0.008562884261753112",
         "-0.008562884261753112",
         "-0.008562884261753112",
         "1995",
         "2"
        ],
        [
         "1995-02-20 00:00:00",
         "533.76",
         "534.57",
         "540.86",
         "534.51",
         "534.96",
         "0.0022482014388489624",
         "0.002245678015396102",
         "0.0022482014388489624",
         "0.0022482014388489624",
         "0.002248201438849006",
         "0.0022482014388489624",
         "0.0022482014388489624",
         "0.0022482014388489624",
         "1995",
         "2"
        ],
        [
         "1995-02-21 00:00:00",
         "534.96",
         "536.07",
         "541.14",
         "535.97",
         "541.14",
         "0.011552265589950483",
         "0.01148604765994854",
         "0.011552265589950483",
         "0.011552265589950483",
         "0.011552265589950556",
         "0.011552265589950483",
         "0.011552265589950483",
         "0.011552265589950483",
         "1995",
         "2"
        ],
        [
         "1995-02-22 00:00:00",
         "541.14",
         "541.21",
         "594.81",
         "541.21",
         "594.64",
         "0.09886535831762577",
         "0.0942781550113656",
         "0.09886535831762577",
         "0.09886535831762577",
         "0.09886535831762576",
         "0.09886535831762577",
         "0.09886535831762577",
         "0.09886535831762577",
         "1995",
         "2"
        ],
        [
         "1995-02-23 00:00:00",
         "594.64",
         "589.9",
         "611.89",
         "575.82",
         "589.13",
         "-0.009266110587918708",
         "-0.00930930804599317",
         "-0.009266110587918708",
         "-0.009266110587918708",
         "-0.009266110587918725",
         "-0.009266110587918708",
         "-0.009266110587918708",
         "-0.009266110587918708",
         "1995",
         "2"
        ],
        [
         "1995-02-24 00:00:00",
         "589.13",
         "591.91",
         "594.92",
         "555.2",
         "558.43",
         "-0.05211073956512158",
         "-0.05351759743764717",
         "-0.05211073956512158",
         "-0.05211073956512158",
         "-0.05211073956512153",
         "-0.05211073956512158",
         "-0.05211073956512158",
         "-0.05211073956512158",
         "1995",
         "2"
        ],
        [
         "1995-02-27 00:00:00",
         "558.43",
         "584.02",
         "591.43",
         "559.04",
         "561.27",
         "0.005085686657235522",
         "0.005072798232039766",
         "0.005085686657235522",
         "0.005085686657235522",
         "0.005085686657235521",
         "0.005085686657235522",
         "0.005085686657235522",
         "0.005085686657235522",
         "1995",
         "2"
        ],
        [
         "1995-02-28 00:00:00",
         "561.27",
         "558.42",
         "558.42",
         "544.06",
         "549.26",
         "-0.021397901188376367",
         "-0.02163015542103075",
         "-0.021397901188376367",
         "-0.021397901188376367",
         "-0.021397901188376346",
         "-0.021397901188376367",
         "-0.021397901188376367",
         "-0.021397901188376367",
         "1995",
         "2"
        ],
        [
         "1995-03-01 00:00:00",
         "549.26",
         "548.91",
         "559.48",
         "545.72",
         "556.96",
         "0.014018861741251998",
         "0.013921506318722798",
         "0.014018861741251998",
         "0.014018861741251998",
         "0.01401886174125195",
         "0.014018861741251998",
         "0.014018861741251998",
         "0.014018861741251998",
         "1995",
         "3"
        ],
        [
         "1995-03-02 00:00:00",
         "556.96",
         "557.5",
         "564.64",
         "553.52",
         "560.85",
         "0.006984343579431229",
         "0.0069600660281032845",
         "0.006984343579431229",
         "0.006984343579431229",
         "0.006984343579431173",
         "0.006984343579431229",
         "0.006984343579431229",
         "0.006984343579431229",
         "1995",
         "3"
        ],
        [
         "1995-03-03 00:00:00",
         "560.85",
         "562.3",
         "580.39",
         "562.3",
         "577.22",
         "0.02918783988588758",
         "0.028769986242834555",
         "0.02918783988588758",
         "0.02918783988588758",
         "0.0291878398858875",
         "0.02918783988588758",
         "0.02918783988588758",
         "0.02918783988588758",
         "1995",
         "3"
        ],
        [
         "1995-03-06 00:00:00",
         "577.22",
         "585.24",
         "616.76",
         "582.55",
         "616.3",
         "0.06770382176639744",
         "0.06551038163413025",
         "0.06770382176639744",
         "0.06770382176639744",
         "0.06770382176639743",
         "0.06770382176639744",
         "0.06770382176639744",
         "0.06770382176639744",
         "1995",
         "3"
        ],
        [
         "1995-03-07 00:00:00",
         "616.3",
         "614.73",
         "635.24",
         "597.2",
         "632.96",
         "0.027032289469414383",
         "0.026673371026095793",
         "0.027032289469414383",
         "0.027032289469414383",
         "0.02703228946941438",
         "0.027032289469414383",
         "0.027032289469414383",
         "0.027032289469414383",
         "1995",
         "3"
        ],
        [
         "1995-03-08 00:00:00",
         "632.96",
         "632.96",
         "634.2",
         "618.8",
         "620.81",
         "-0.019195525783619938",
         "-0.019382152007597853",
         "-0.019195525783619938",
         "-0.019195525783619938",
         "-0.019195525783619962",
         "-0.019195525783619938",
         "-0.019195525783619938",
         "-0.019195525783619938",
         "1995",
         "3"
        ],
        [
         "1995-03-09 00:00:00",
         "620.81",
         "617.22",
         "624.25",
         "604.79",
         "605.59",
         "-0.0245163576617643",
         "-0.02482188754932757",
         "-0.0245163576617643",
         "-0.0245163576617643",
         "-0.024516357661764333",
         "-0.0245163576617643",
         "-0.0245163576617643",
         "-0.0245163576617643",
         "1995",
         "3"
        ],
        [
         "1995-03-10 00:00:00",
         "605.59",
         "600.45",
         "600.51",
         "583.72",
         "586.29",
         "-0.03186974685843569",
         "-0.032388641727272294",
         "-0.03186974685843569",
         "-0.03186974685843569",
         "-0.03186974685843569",
         "-0.03186974685843569",
         "-0.03186974685843569",
         "-0.03186974685843569",
         "1995",
         "3"
        ],
        [
         "1995-03-13 00:00:00",
         "586.29",
         "596.65",
         "597.18",
         "583.38",
         "586.23",
         "-0.00010233843319851221",
         "-0.00010234367013328693",
         "-0.00010233843319851221",
         "-0.00010233843319851221",
         "-0.00010233843319849466",
         "-0.00010233843319851221",
         "-0.00010233843319851221",
         "-0.00010233843319851221",
         "1995",
         "3"
        ],
        [
         "1995-03-14 00:00:00",
         "586.23",
         "588.18",
         "599.9",
         "586.19",
         "588.24",
         "0.003428688398751323",
         "0.0034228238480045192",
         "0.003428688398751323",
         "0.003428688398751323",
         "0.0034286883987513276",
         "0.003428688398751323",
         "0.003428688398751323",
         "0.003428688398751323",
         "1995",
         "3"
        ],
        [
         "1995-03-15 00:00:00",
         "588.24",
         "588.78",
         "591.49",
         "568.6",
         "572.6",
         "-0.026587787297701615",
         "-0.026947635223951494",
         "-0.026587787297701615",
         "-0.026587787297701615",
         "-0.026587787297701594",
         "-0.026587787297701615",
         "-0.026587787297701615",
         "-0.026587787297701615",
         "1995",
         "3"
        ],
        [
         "1995-03-16 00:00:00",
         "572.6",
         "570.51",
         "579.88",
         "569.17",
         "579.88",
         "0.012713936430317707",
         "0.012633792918681941",
         "0.012713936430317707",
         "0.012713936430317707",
         "0.0127139364303178",
         "0.012713936430317707",
         "0.012713936430317707",
         "0.012713936430317707",
         "1995",
         "3"
        ],
        [
         "1995-03-17 00:00:00",
         "579.88",
         "579.62",
         "593.97",
         "577.91",
         "593.52",
         "0.02352210802234933",
         "0.023249726298573137",
         "0.02352210802234933",
         "0.02352210802234933",
         "0.023522108022349428",
         "0.02352210802234933",
         "0.02352210802234933",
         "0.02352210802234933",
         "1995",
         "3"
        ],
        [
         "1995-03-20 00:00:00",
         "593.52",
         "597.85",
         "604.96",
         "585.15",
         "585.45",
         "-0.01359684593610988",
         "-0.013690129586536592",
         "-0.01359684593610988",
         "-0.01359684593610988",
         "-0.01359684593610988",
         "-0.01359684593610988",
         "-0.01359684593610988",
         "-0.01359684593610988",
         "1995",
         "3"
        ]
       ],
       "shape": {
        "columns": 15,
        "rows": 7445
       }
      },
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Preclose</th>\n",
       "      <th>Open</th>\n",
       "      <th>Highest</th>\n",
       "      <th>Lowest</th>\n",
       "      <th>Close</th>\n",
       "      <th>Raw_return</th>\n",
       "      <th>Log_return</th>\n",
       "      <th>Pct_change_return</th>\n",
       "      <th>Apply_return</th>\n",
       "      <th>Diff_div_return</th>\n",
       "      <th>Loop_return</th>\n",
       "      <th>Loop_return2</th>\n",
       "      <th>Numpy_return</th>\n",
       "      <th>year</th>\n",
       "      <th>month</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Day</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>1995-01-03</th>\n",
       "      <td>647.870</td>\n",
       "      <td>637.720</td>\n",
       "      <td>647.710</td>\n",
       "      <td>630.530</td>\n",
       "      <td>639.880</td>\n",
       "      <td>-0.012333</td>\n",
       "      <td>-0.012409</td>\n",
       "      <td>NaN</td>\n",
       "      <td>-0.012333</td>\n",
       "      <td>NaN</td>\n",
       "      <td>-0.012333</td>\n",
       "      <td>-0.012333</td>\n",
       "      <td>-0.012333</td>\n",
       "      <td>1995</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1995-01-04</th>\n",
       "      <td>639.880</td>\n",
       "      <td>641.900</td>\n",
       "      <td>655.510</td>\n",
       "      <td>638.860</td>\n",
       "      <td>653.810</td>\n",
       "      <td>0.021770</td>\n",
       "      <td>0.021536</td>\n",
       "      <td>0.021770</td>\n",
       "      <td>0.021770</td>\n",
       "      <td>0.021770</td>\n",
       "      <td>0.021770</td>\n",
       "      <td>0.021770</td>\n",
       "      <td>0.021770</td>\n",
       "      <td>1995</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1995-01-05</th>\n",
       "      <td>653.810</td>\n",
       "      <td>655.380</td>\n",
       "      <td>657.520</td>\n",
       "      <td>645.810</td>\n",
       "      <td>646.890</td>\n",
       "      <td>-0.010584</td>\n",
       "      <td>-0.010641</td>\n",
       "      <td>-0.010584</td>\n",
       "      <td>-0.010584</td>\n",
       "      <td>-0.010584</td>\n",
       "      <td>-0.010584</td>\n",
       "      <td>-0.010584</td>\n",
       "      <td>-0.010584</td>\n",
       "      <td>1995</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1995-01-06</th>\n",
       "      <td>646.890</td>\n",
       "      <td>642.750</td>\n",
       "      <td>643.890</td>\n",
       "      <td>636.330</td>\n",
       "      <td>640.760</td>\n",
       "      <td>-0.009476</td>\n",
       "      <td>-0.009521</td>\n",
       "      <td>-0.009476</td>\n",
       "      <td>-0.009476</td>\n",
       "      <td>-0.009476</td>\n",
       "      <td>-0.009476</td>\n",
       "      <td>-0.009476</td>\n",
       "      <td>-0.009476</td>\n",
       "      <td>1995</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1995-01-09</th>\n",
       "      <td>640.760</td>\n",
       "      <td>637.520</td>\n",
       "      <td>637.550</td>\n",
       "      <td>625.040</td>\n",
       "      <td>626.000</td>\n",
       "      <td>-0.023035</td>\n",
       "      <td>-0.023305</td>\n",
       "      <td>-0.023035</td>\n",
       "      <td>-0.023035</td>\n",
       "      <td>-0.023035</td>\n",
       "      <td>-0.023035</td>\n",
       "      <td>-0.023035</td>\n",
       "      <td>-0.023035</td>\n",
       "      <td>1995</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2025-08-25</th>\n",
       "      <td>3825.759</td>\n",
       "      <td>3848.163</td>\n",
       "      <td>3883.562</td>\n",
       "      <td>3839.972</td>\n",
       "      <td>3883.562</td>\n",
       "      <td>0.015109</td>\n",
       "      <td>0.014996</td>\n",
       "      <td>0.015109</td>\n",
       "      <td>0.015109</td>\n",
       "      <td>0.015109</td>\n",
       "      <td>0.015109</td>\n",
       "      <td>0.015109</td>\n",
       "      <td>0.015109</td>\n",
       "      <td>2025</td>\n",
       "      <td>8</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2025-08-26</th>\n",
       "      <td>3883.562</td>\n",
       "      <td>3871.471</td>\n",
       "      <td>3888.599</td>\n",
       "      <td>3859.758</td>\n",
       "      <td>3868.382</td>\n",
       "      <td>-0.003909</td>\n",
       "      <td>-0.003916</td>\n",
       "      <td>-0.003909</td>\n",
       "      <td>-0.003909</td>\n",
       "      <td>-0.003909</td>\n",
       "      <td>-0.003909</td>\n",
       "      <td>-0.003909</td>\n",
       "      <td>-0.003909</td>\n",
       "      <td>2025</td>\n",
       "      <td>8</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2025-08-27</th>\n",
       "      <td>3868.382</td>\n",
       "      <td>3869.612</td>\n",
       "      <td>3887.198</td>\n",
       "      <td>3800.350</td>\n",
       "      <td>3800.350</td>\n",
       "      <td>-0.017587</td>\n",
       "      <td>-0.017743</td>\n",
       "      <td>-0.017587</td>\n",
       "      <td>-0.017587</td>\n",
       "      <td>-0.017587</td>\n",
       "      <td>-0.017587</td>\n",
       "      <td>-0.017587</td>\n",
       "      <td>-0.017587</td>\n",
       "      <td>2025</td>\n",
       "      <td>8</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2025-08-28</th>\n",
       "      <td>3800.350</td>\n",
       "      <td>3796.711</td>\n",
       "      <td>3845.087</td>\n",
       "      <td>3761.422</td>\n",
       "      <td>3843.597</td>\n",
       "      <td>0.011380</td>\n",
       "      <td>0.011315</td>\n",
       "      <td>0.011380</td>\n",
       "      <td>0.011380</td>\n",
       "      <td>0.011380</td>\n",
       "      <td>0.011380</td>\n",
       "      <td>0.011380</td>\n",
       "      <td>0.011380</td>\n",
       "      <td>2025</td>\n",
       "      <td>8</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2025-08-29</th>\n",
       "      <td>3843.597</td>\n",
       "      <td>3842.823</td>\n",
       "      <td>3867.606</td>\n",
       "      <td>3839.206</td>\n",
       "      <td>3857.927</td>\n",
       "      <td>0.003728</td>\n",
       "      <td>0.003721</td>\n",
       "      <td>0.003728</td>\n",
       "      <td>0.003728</td>\n",
       "      <td>0.003728</td>\n",
       "      <td>0.003728</td>\n",
       "      <td>0.003728</td>\n",
       "      <td>0.003728</td>\n",
       "      <td>2025</td>\n",
       "      <td>8</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>7445 rows × 15 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "            Preclose      Open   Highest    Lowest     Close  Raw_return  \\\n",
       "Day                                                                        \n",
       "1995-01-03   647.870   637.720   647.710   630.530   639.880   -0.012333   \n",
       "1995-01-04   639.880   641.900   655.510   638.860   653.810    0.021770   \n",
       "1995-01-05   653.810   655.380   657.520   645.810   646.890   -0.010584   \n",
       "1995-01-06   646.890   642.750   643.890   636.330   640.760   -0.009476   \n",
       "1995-01-09   640.760   637.520   637.550   625.040   626.000   -0.023035   \n",
       "...              ...       ...       ...       ...       ...         ...   \n",
       "2025-08-25  3825.759  3848.163  3883.562  3839.972  3883.562    0.015109   \n",
       "2025-08-26  3883.562  3871.471  3888.599  3859.758  3868.382   -0.003909   \n",
       "2025-08-27  3868.382  3869.612  3887.198  3800.350  3800.350   -0.017587   \n",
       "2025-08-28  3800.350  3796.711  3845.087  3761.422  3843.597    0.011380   \n",
       "2025-08-29  3843.597  3842.823  3867.606  3839.206  3857.927    0.003728   \n",
       "\n",
       "            Log_return  Pct_change_return  Apply_return  Diff_div_return  \\\n",
       "Day                                                                        \n",
       "1995-01-03   -0.012409                NaN     -0.012333              NaN   \n",
       "1995-01-04    0.021536           0.021770      0.021770         0.021770   \n",
       "1995-01-05   -0.010641          -0.010584     -0.010584        -0.010584   \n",
       "1995-01-06   -0.009521          -0.009476     -0.009476        -0.009476   \n",
       "1995-01-09   -0.023305          -0.023035     -0.023035        -0.023035   \n",
       "...                ...                ...           ...              ...   \n",
       "2025-08-25    0.014996           0.015109      0.015109         0.015109   \n",
       "2025-08-26   -0.003916          -0.003909     -0.003909        -0.003909   \n",
       "2025-08-27   -0.017743          -0.017587     -0.017587        -0.017587   \n",
       "2025-08-28    0.011315           0.011380      0.011380         0.011380   \n",
       "2025-08-29    0.003721           0.003728      0.003728         0.003728   \n",
       "\n",
       "            Loop_return  Loop_return2  Numpy_return  year  month  \n",
       "Day                                                               \n",
       "1995-01-03    -0.012333     -0.012333     -0.012333  1995      1  \n",
       "1995-01-04     0.021770      0.021770      0.021770  1995      1  \n",
       "1995-01-05    -0.010584     -0.010584     -0.010584  1995      1  \n",
       "1995-01-06    -0.009476     -0.009476     -0.009476  1995      1  \n",
       "1995-01-09    -0.023035     -0.023035     -0.023035  1995      1  \n",
       "...                 ...           ...           ...   ...    ...  \n",
       "2025-08-25     0.015109      0.015109      0.015109  2025      8  \n",
       "2025-08-26    -0.003909     -0.003909     -0.003909  2025      8  \n",
       "2025-08-27    -0.017587     -0.017587     -0.017587  2025      8  \n",
       "2025-08-28     0.011380      0.011380      0.011380  2025      8  \n",
       "2025-08-29     0.003728      0.003728      0.003728  2025      8  \n",
       "\n",
       "[7445 rows x 15 columns]"
      ]
     },
     "execution_count": 49,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "application/vnd.microsoft.datawrangler.viewer.v0+json": {
       "columns": [
        {
         "name": "('year', 'month')",
         "rawType": "object",
         "type": "unknown"
        },
        {
         "name": "Log_return",
         "rawType": "float64",
         "type": "float"
        },
        {
         "name": "Raw_Return",
         "rawType": "float64",
         "type": "float"
        }
       ],
       "ref": "7aa1337c-1e98-44f7-a731-b5cf41bc0a8e",
       "rows": [
        [
         "(np.int32(1995), np.int32(1))",
         "-0.14113893766204466",
         "-0.13163134579468116"
        ],
        [
         "(np.int32(1995), np.int32(2))",
         "-0.023979203535533955",
         "-0.023693986739899198"
        ],
        [
         "(np.int32(1995), np.int32(3))",
         "0.16365072150717097",
         "0.17780286203255224"
        ],
        [
         "(np.int32(1995), np.int32(4))",
         "-0.10931523264986698",
         "-0.10355221665739145"
        ],
        [
         "(np.int32(1995), np.int32(5))",
         "0.18890123459094443",
         "0.2079216457158617"
        ],
        [
         "(np.int32(1995), np.int32(6))",
         "-0.1051686104108489",
         "-0.09982726870422942"
        ],
        [
         "(np.int32(1995), np.int32(7))",
         "0.09806286872377346",
         "0.10303212915093996"
        ],
        [
         "(np.int32(1995), np.int32(8))",
         "0.03990891874661795",
         "0.04071598015958644"
        ],
        [
         "(np.int32(1995), np.int32(9))",
         "-0.001991288772492439",
         "-0.001989307472336499"
        ],
        [
         "(np.int32(1995), np.int32(10))",
         "-0.007098484399481464",
         "-0.00707334966709583"
        ],
        [
         "(np.int32(1995), np.int32(11))",
         "-0.11227420327091941",
         "-0.10620085875202112"
        ],
        [
         "(np.int32(1995), np.int32(12))",
         "-0.14375734200770385",
         "-0.13390211186324363"
        ],
        [
         "(np.int32(1996), np.int32(1))",
         "-0.03284084851443758",
         "-0.0323074429577338"
        ],
        [
         "(np.int32(1996), np.int32(2))",
         "0.028599845252879064",
         "0.029012747743556444"
        ],
        [
         "(np.int32(1996), np.int32(3))",
         "0.0062199906662119275",
         "0.006239374977393464"
        ],
        [
         "(np.int32(1996), np.int32(4))",
         "0.2023277399521506",
         "0.22424917773504172"
        ],
        [
         "(np.int32(1996), np.int32(5))",
         "-0.056642127052760394",
         "-0.05506782547419098"
        ],
        [
         "(np.int32(1996), np.int32(6))",
         "0.22275506601349182",
         "0.24951448768740714"
        ],
        [
         "(np.int32(1996), np.int32(7))",
         "0.02241400011532768",
         "0.022667081131489297"
        ],
        [
         "(np.int32(1996), np.int32(8))",
         "-0.015363995385672347",
         "-0.0152465713451998"
        ],
        [
         "(np.int32(1996), np.int32(9))",
         "0.07786924642368298",
         "0.08098130725732733"
        ],
        [
         "(np.int32(1996), np.int32(10))",
         "0.1093603636869549",
         "0.11556428677486807"
        ],
        [
         "(np.int32(1996), np.int32(11))",
         "0.05598428427820945",
         "0.05758106295625143"
        ],
        [
         "(np.int32(1996), np.int32(12))",
         "-0.11904478296819576",
         "-0.11223195701631306"
        ],
        [
         "(np.int32(1997), np.int32(1))",
         "0.050729352714932396",
         "0.0520381234869467"
        ],
        [
         "(np.int32(1997), np.int32(2))",
         "0.0753769388414165",
         "0.07829052387171709"
        ],
        [
         "(np.int32(1997), np.int32(3))",
         "0.171282935581603",
         "0.18682649696713316"
        ],
        [
         "(np.int32(1997), np.int32(4))",
         "0.12123472580103467",
         "0.1288898608478728"
        ],
        [
         "(np.int32(1997), np.int32(5))",
         "-0.08109916986807875",
         "-0.0778977578475335"
        ],
        [
         "(np.int32(1997), np.int32(6))",
         "-0.02753925836864468",
         "-0.027163510169781913"
        ],
        [
         "(np.int32(1997), np.int32(7))",
         "-0.049607921878680905",
         "-0.048397546130035884"
        ],
        [
         "(np.int32(1997), np.int32(8))",
         "0.025967727859486978",
         "0.026307826788595445"
        ],
        [
         "(np.int32(1997), np.int32(9))",
         "-0.10679381338891858",
         "-0.10128904394542382"
        ],
        [
         "(np.int32(1997), np.int32(10))",
         "0.07291937176479912",
         "0.07564380615648125"
        ],
        [
         "(np.int32(1997), np.int32(11))",
         "-0.035141244024811336",
         "-0.03453096010640533"
        ],
        [
         "(np.int32(1997), np.int32(12))",
         "0.04668911523575758",
         "0.04779621456086569"
        ],
        [
         "(np.int32(1998), np.int32(1))",
         "0.023840500906058715",
         "0.024126957541245275"
        ],
        [
         "(np.int32(1998), np.int32(2))",
         "-0.013484793376559168",
         "-0.013394280854683194"
        ],
        [
         "(np.int32(1998), np.int32(3))",
         "0.02979543141701413",
         "0.030243756889592355"
        ],
        [
         "(np.int32(1998), np.int32(4))",
         "0.07769702965087788",
         "0.0807951601744139"
        ],
        [
         "(np.int32(1998), np.int32(5))",
         "0.04920656025849013",
         "0.05043730693364101"
        ],
        [
         "(np.int32(1998), np.int32(6))",
         "-0.0523750716600091",
         "-0.0510271327442402"
        ],
        [
         "(np.int32(1998), np.int32(7))",
         "-0.01677674395234252",
         "-0.016636798088411342"
        ],
        [
         "(np.int32(1998), np.int32(8))",
         "-0.13534244837409037",
         "-0.1265832396804667"
        ],
        [
         "(np.int32(1998), np.int32(9))",
         "0.07749413034268748",
         "0.08057588982977193"
        ],
        [
         "(np.int32(1998), np.int32(10))",
         "-0.020795637667219147",
         "-0.02058089950921238"
        ],
        [
         "(np.int32(1998), np.int32(11))",
         "0.024425697228609877",
         "0.02472644826339887"
        ],
        [
         "(np.int32(1998), np.int32(12))",
         "-0.0841891662685974",
         "-0.08074265283545234"
        ],
        [
         "(np.int32(1999), np.int32(1))",
         "-0.01054639230309018",
         "-0.010490974099590544"
        ],
        [
         "(np.int32(1999), np.int32(2))",
         "-0.04008159811925616",
         "-0.03928895626041018"
        ]
       ],
       "shape": {
        "columns": 2,
        "rows": 368
       }
      },
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th>Log_return</th>\n",
       "      <th>Raw_Return</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>year</th>\n",
       "      <th>month</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th rowspan=\"5\" valign=\"top\">1995</th>\n",
       "      <th>1</th>\n",
       "      <td>-0.141139</td>\n",
       "      <td>-0.131631</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>-0.023979</td>\n",
       "      <td>-0.023694</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>0.163651</td>\n",
       "      <td>0.177803</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>-0.109315</td>\n",
       "      <td>-0.103552</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>0.188901</td>\n",
       "      <td>0.207922</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"5\" valign=\"top\">2025</th>\n",
       "      <th>4</th>\n",
       "      <td>-0.017148</td>\n",
       "      <td>-0.017002</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>0.020662</td>\n",
       "      <td>0.020877</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>0.028547</td>\n",
       "      <td>0.028959</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>0.036707</td>\n",
       "      <td>0.037389</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>0.076666</td>\n",
       "      <td>0.079682</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>368 rows × 2 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "            Log_return  Raw_Return\n",
       "year month                        \n",
       "1995 1       -0.141139   -0.131631\n",
       "     2       -0.023979   -0.023694\n",
       "     3        0.163651    0.177803\n",
       "     4       -0.109315   -0.103552\n",
       "     5        0.188901    0.207922\n",
       "...                ...         ...\n",
       "2025 4       -0.017148   -0.017002\n",
       "     5        0.020662    0.020877\n",
       "     6        0.028547    0.028959\n",
       "     7        0.036707    0.037389\n",
       "     8        0.076666    0.079682\n",
       "\n",
       "[368 rows x 2 columns]"
      ]
     },
     "execution_count": 49,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# “1990-12-12”日期格式 里面的year年份 month月份 day 直接提出取来\n",
    "data_new2 = data_new.copy()\n",
    "data_new2['year'] = data_new2.index.year\n",
    "data_new2['month'] = data_new2.index.month\n",
    "data_new2\n",
    "# 使用的时间、日期格式提取 字符串提出的方式 前四个字符当作年份 6-7字符是月份 提取出来的是字符串 变成数值# 方法3：使用groupby函数按年月分组计算月度收益率\n",
    "# 首先提取年月信息\n",
    "data_new3 = data_new.copy()\n",
    "data_new3['year'] = data_new3.index.year\n",
    "data_new3['month'] = data_new3.index.month\n",
    "\n",
    "# 使用groupby按年月分组，然后对每组的对数收益率求和\n",
    "Month_data3 = data_new3.groupby(['year', 'month'])['Log_return'].sum().to_frame()\n",
    "Month_data3['Raw_Return'] = np.exp(Month_data3['Log_return']) - 1\n",
    "\n",
    "# 显示结果\n",
    "Month_data3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "id": "23ff2695",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.microsoft.datawrangler.viewer.v0+json": {
       "columns": [
        {
         "name": "('year', 'month')",
         "rawType": "object",
         "type": "unknown"
        },
        {
         "name": "Log_return",
         "rawType": "float64",
         "type": "float"
        },
        {
         "name": "Raw_Return",
         "rawType": "float64",
         "type": "float"
        }
       ],
       "ref": "bcd9491f-dfd1-4cfc-8563-b2bf2d0216a3",
       "rows": [
        [
         "(np.int32(1995), np.int32(1))",
         "-0.14113893766204466",
         "-0.13163134579468116"
        ],
        [
         "(np.int32(1995), np.int32(2))",
         "-0.023979203535533955",
         "-0.023693986739899198"
        ],
        [
         "(np.int32(1995), np.int32(3))",
         "0.16365072150717097",
         "0.17780286203255224"
        ],
        [
         "(np.int32(1995), np.int32(4))",
         "-0.10931523264986698",
         "-0.10355221665739145"
        ],
        [
         "(np.int32(1995), np.int32(5))",
         "0.18890123459094443",
         "0.2079216457158617"
        ],
        [
         "(np.int32(1995), np.int32(6))",
         "-0.1051686104108489",
         "-0.09982726870422942"
        ],
        [
         "(np.int32(1995), np.int32(7))",
         "0.09806286872377346",
         "0.10303212915093996"
        ],
        [
         "(np.int32(1995), np.int32(8))",
         "0.03990891874661795",
         "0.04071598015958644"
        ],
        [
         "(np.int32(1995), np.int32(9))",
         "-0.001991288772492439",
         "-0.001989307472336499"
        ],
        [
         "(np.int32(1995), np.int32(10))",
         "-0.007098484399481464",
         "-0.00707334966709583"
        ],
        [
         "(np.int32(1995), np.int32(11))",
         "-0.11227420327091941",
         "-0.10620085875202112"
        ],
        [
         "(np.int32(1995), np.int32(12))",
         "-0.14375734200770385",
         "-0.13390211186324363"
        ],
        [
         "(np.int32(1996), np.int32(1))",
         "-0.03284084851443758",
         "-0.0323074429577338"
        ],
        [
         "(np.int32(1996), np.int32(2))",
         "0.028599845252879064",
         "0.029012747743556444"
        ],
        [
         "(np.int32(1996), np.int32(3))",
         "0.0062199906662119275",
         "0.006239374977393464"
        ],
        [
         "(np.int32(1996), np.int32(4))",
         "0.2023277399521506",
         "0.22424917773504172"
        ],
        [
         "(np.int32(1996), np.int32(5))",
         "-0.056642127052760394",
         "-0.05506782547419098"
        ],
        [
         "(np.int32(1996), np.int32(6))",
         "0.22275506601349182",
         "0.24951448768740714"
        ],
        [
         "(np.int32(1996), np.int32(7))",
         "0.02241400011532768",
         "0.022667081131489297"
        ],
        [
         "(np.int32(1996), np.int32(8))",
         "-0.015363995385672347",
         "-0.0152465713451998"
        ],
        [
         "(np.int32(1996), np.int32(9))",
         "0.07786924642368298",
         "0.08098130725732733"
        ],
        [
         "(np.int32(1996), np.int32(10))",
         "0.1093603636869549",
         "0.11556428677486807"
        ],
        [
         "(np.int32(1996), np.int32(11))",
         "0.05598428427820945",
         "0.05758106295625143"
        ],
        [
         "(np.int32(1996), np.int32(12))",
         "-0.11904478296819576",
         "-0.11223195701631306"
        ],
        [
         "(np.int32(1997), np.int32(1))",
         "0.050729352714932396",
         "0.0520381234869467"
        ],
        [
         "(np.int32(1997), np.int32(2))",
         "0.0753769388414165",
         "0.07829052387171709"
        ],
        [
         "(np.int32(1997), np.int32(3))",
         "0.171282935581603",
         "0.18682649696713316"
        ],
        [
         "(np.int32(1997), np.int32(4))",
         "0.12123472580103467",
         "0.1288898608478728"
        ],
        [
         "(np.int32(1997), np.int32(5))",
         "-0.08109916986807875",
         "-0.0778977578475335"
        ],
        [
         "(np.int32(1997), np.int32(6))",
         "-0.02753925836864468",
         "-0.027163510169781913"
        ],
        [
         "(np.int32(1997), np.int32(7))",
         "-0.049607921878680905",
         "-0.048397546130035884"
        ],
        [
         "(np.int32(1997), np.int32(8))",
         "0.025967727859486978",
         "0.026307826788595445"
        ],
        [
         "(np.int32(1997), np.int32(9))",
         "-0.10679381338891858",
         "-0.10128904394542382"
        ],
        [
         "(np.int32(1997), np.int32(10))",
         "0.07291937176479912",
         "0.07564380615648125"
        ],
        [
         "(np.int32(1997), np.int32(11))",
         "-0.035141244024811336",
         "-0.03453096010640533"
        ],
        [
         "(np.int32(1997), np.int32(12))",
         "0.04668911523575758",
         "0.04779621456086569"
        ],
        [
         "(np.int32(1998), np.int32(1))",
         "0.023840500906058715",
         "0.024126957541245275"
        ],
        [
         "(np.int32(1998), np.int32(2))",
         "-0.013484793376559168",
         "-0.013394280854683194"
        ],
        [
         "(np.int32(1998), np.int32(3))",
         "0.02979543141701413",
         "0.030243756889592355"
        ],
        [
         "(np.int32(1998), np.int32(4))",
         "0.07769702965087788",
         "0.0807951601744139"
        ],
        [
         "(np.int32(1998), np.int32(5))",
         "0.04920656025849013",
         "0.05043730693364101"
        ],
        [
         "(np.int32(1998), np.int32(6))",
         "-0.0523750716600091",
         "-0.0510271327442402"
        ],
        [
         "(np.int32(1998), np.int32(7))",
         "-0.01677674395234252",
         "-0.016636798088411342"
        ],
        [
         "(np.int32(1998), np.int32(8))",
         "-0.13534244837409037",
         "-0.1265832396804667"
        ],
        [
         "(np.int32(1998), np.int32(9))",
         "0.07749413034268748",
         "0.08057588982977193"
        ],
        [
         "(np.int32(1998), np.int32(10))",
         "-0.020795637667219147",
         "-0.02058089950921238"
        ],
        [
         "(np.int32(1998), np.int32(11))",
         "0.024425697228609877",
         "0.02472644826339887"
        ],
        [
         "(np.int32(1998), np.int32(12))",
         "-0.0841891662685974",
         "-0.08074265283545234"
        ],
        [
         "(np.int32(1999), np.int32(1))",
         "-0.01054639230309018",
         "-0.010490974099590544"
        ],
        [
         "(np.int32(1999), np.int32(2))",
         "-0.04008159811925616",
         "-0.03928895626041018"
        ]
       ],
       "shape": {
        "columns": 2,
        "rows": 368
       }
      },
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th>Log_return</th>\n",
       "      <th>Raw_Return</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>year</th>\n",
       "      <th>month</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th rowspan=\"5\" valign=\"top\">1995</th>\n",
       "      <th>1</th>\n",
       "      <td>-0.141139</td>\n",
       "      <td>-0.131631</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>-0.023979</td>\n",
       "      <td>-0.023694</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>0.163651</td>\n",
       "      <td>0.177803</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>-0.109315</td>\n",
       "      <td>-0.103552</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>0.188901</td>\n",
       "      <td>0.207922</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"5\" valign=\"top\">2025</th>\n",
       "      <th>4</th>\n",
       "      <td>-0.017148</td>\n",
       "      <td>-0.017002</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>0.020662</td>\n",
       "      <td>0.020877</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>0.028547</td>\n",
       "      <td>0.028959</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>0.036707</td>\n",
       "      <td>0.037389</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>0.076666</td>\n",
       "      <td>0.079682</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>368 rows × 2 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "            Log_return  Raw_Return\n",
       "year month                        \n",
       "1995 1       -0.141139   -0.131631\n",
       "     2       -0.023979   -0.023694\n",
       "     3        0.163651    0.177803\n",
       "     4       -0.109315   -0.103552\n",
       "     5        0.188901    0.207922\n",
       "...                ...         ...\n",
       "2025 4       -0.017148   -0.017002\n",
       "     5        0.020662    0.020877\n",
       "     6        0.028547    0.028959\n",
       "     7        0.036707    0.037389\n",
       "     8        0.076666    0.079682\n",
       "\n",
       "[368 rows x 2 columns]"
      ]
     },
     "execution_count": 50,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 方法3：使用groupby函数按年月分组计算月度收益率\n",
    "# 首先提取年月信息\n",
    "data_new3 = data_new.copy()\n",
    "data_new3['year'] = data_new3.index.year\n",
    "data_new3['month'] = data_new3.index.month\n",
    "\n",
    "# 使用groupby按年月分组，然后对每组的对数收益率求和\n",
    "Month_data3 = data_new3.groupby(['year', 'month'])['Log_return'].sum().to_frame()\n",
    "Month_data3['Raw_Return'] = np.exp(Month_data3['Log_return']) - 1\n",
    "\n",
    "# 显示结果\n",
    "Month_data3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "id": "78a9bea3",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "方法4结果:\n",
      "            Log_return  Raw_Return\n",
      "year month                        \n",
      "1995 1       -0.141139   -0.131631\n",
      "     2       -0.023979   -0.023694\n",
      "     3        0.163651    0.177803\n",
      "     4       -0.109315   -0.103552\n",
      "     5        0.188901    0.207922\n",
      "\n",
      "方法5结果 (包含多个统计量):\n",
      "           Log_return                           Raw_return          \n",
      "                  sum      mean       std count       mean       std\n",
      "year month                                                          \n",
      "1995 1      -0.141139 -0.007428  0.016251    19  -0.007277  0.016140\n",
      "     2      -0.023979 -0.001411  0.033003    17  -0.000891  0.033609\n",
      "     3       0.163651  0.007115  0.023204    23   0.007401  0.023470\n",
      "     4      -0.109315 -0.005466  0.020374    20  -0.005255  0.020169\n",
      "     5       0.188901  0.008586  0.077844    22   0.011645  0.083211\n"
     ]
    }
   ],
   "source": [
    "# 方法4：使用apply和lambda函数进行更灵活的分组计算\n",
    "# 这种方法可以对每个月的数据进行更复杂的操作\n",
    "Month_data4 = pd.DataFrame(\n",
    "    data_new3.groupby(['year', 'month'])['Log_return'].apply(lambda x: sum(x)))\n",
    "Month_data4.columns = ['Log_return']\n",
    "Month_data4['Raw_Return'] = np.exp(Month_data4['Log_return']) - 1\n",
    "\n",
    "# 方法5：使用agg函数同时计算多个统计量\n",
    "Month_data5 = data_new3.groupby(['year', 'month']).agg({\n",
    "    'Log_return': ['sum', 'mean', 'std', 'count'],\n",
    "    'Raw_return': ['mean', 'std']\n",
    "})\n",
    "\n",
    "# 显示结果\n",
    "print(\"方法4结果:\")\n",
    "print(Month_data4.head())\n",
    "print(\"\\n方法5结果 (包含多个统计量):\")\n",
    "print(Month_data5.head())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "id": "14ce8e45",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "季度对数收益率汇总:\n"
     ]
    },
    {
     "data": {
      "application/vnd.microsoft.datawrangler.viewer.v0+json": {
       "columns": [
        {
         "name": "Day",
         "rawType": "datetime64[ns]",
         "type": "datetime"
        },
        {
         "name": "Log_return",
         "rawType": "float64",
         "type": "float"
        },
        {
         "name": "Raw_Return",
         "rawType": "float64",
         "type": "float"
        },
        {
         "name": "Year",
         "rawType": "int32",
         "type": "integer"
        },
        {
         "name": "Quarter",
         "rawType": "int32",
         "type": "integer"
        }
       ],
       "ref": "71f2fc4e-bbd8-4e07-978c-dd87bcfb4fe8",
       "rows": [
        [
         "1995-03-31 00:00:00",
         "-0.0014674196904076453",
         "-0.0014663435565781535",
         "1995",
         "1"
        ],
        [
         "1995-06-30 00:00:00",
         "-0.02558260846977145",
         "-0.02525814629320422",
         "1995",
         "2"
        ],
        [
         "1995-09-30 00:00:00",
         "0.13598049869789897",
         "0.14565955152399357",
         "1995",
         "3"
        ],
        [
         "1995-12-31 00:00:00",
         "-0.2631300296781047",
         "-0.23135805545173904",
         "1995",
         "4"
        ],
        [
         "1996-03-31 00:00:00",
         "0.0019789874046534095",
         "0.0019809468926148543",
         "1996",
         "1"
        ],
        [
         "1996-06-30 00:00:00",
         "0.368440678912882",
         "0.44547889070615865",
         "1996",
         "2"
        ],
        [
         "1996-09-30 00:00:00",
         "0.08491925115333832",
         "0.08862915760024892",
         "1996",
         "3"
        ],
        [
         "1996-12-31 00:00:00",
         "0.04629986499696859",
         "0.0473884390026611",
         "1996",
         "4"
        ],
        [
         "1997-03-31 00:00:00",
         "0.2973892271379519",
         "0.34633922924254623",
         "1997",
         "1"
        ],
        [
         "1997-06-30 00:00:00",
         "0.012596297564311243",
         "0.012675965074274265",
         "1997",
         "2"
        ],
        [
         "1997-09-30 00:00:00",
         "-0.1304340074081125",
         "-0.12228558631335606",
         "1997",
         "3"
        ],
        [
         "1997-12-31 00:00:00",
         "0.08446724297574537",
         "0.08813719951156296",
         "1997",
         "4"
        ],
        [
         "1998-03-31 00:00:00",
         "0.040151138946513676",
         "0.04096809312452909",
         "1998",
         "1"
        ],
        [
         "1998-06-30 00:00:00",
         "0.07452851824935891",
         "0.07737606796350871",
         "1998",
         "2"
        ],
        [
         "1998-09-30 00:00:00",
         "-0.07462506198374541",
         "-0.07190860215053796",
         "1998",
         "3"
        ],
        [
         "1998-12-31 00:00:00",
         "-0.08055910670720667",
         "-0.07739962989781923",
         "1998",
         "4"
        ],
        [
         "1999-03-31 00:00:00",
         "0.009849304049462049",
         "0.009897968082323017",
         "1999",
         "1"
        ],
        [
         "1999-06-30 00:00:00",
         "0.37765363782583616",
         "0.45885756228142127",
         "1999",
         "2"
        ],
        [
         "1999-09-30 00:00:00",
         "-0.0728698140705708",
         "-0.07027814114819808",
         "1999",
         "3"
        ],
        [
         "1999-12-31 00:00:00",
         "-0.13921011146963647",
         "-0.12995479722416758",
         "1999",
         "4"
        ],
        [
         "2000-03-31 00:00:00",
         "0.2755976112586014",
         "0.31731768356042167",
         "2000",
         "1"
        ],
        [
         "2000-06-30 00:00:00",
         "0.06863136884693422",
         "0.07104131717234541",
         "2000",
         "2"
        ],
        [
         "2000-09-30 00:00:00",
         "-0.009353240319182277",
         "-0.00930963482373981",
         "2000",
         "3"
        ],
        [
         "2000-12-31 00:00:00",
         "0.08204134694732712",
         "0.08550069104158808",
         "2000",
         "4"
        ],
        [
         "2001-03-31 00:00:00",
         "0.018776260713766924",
         "0.01895364315064585",
         "2001",
         "1"
        ],
        [
         "2001-06-30 00:00:00",
         "0.048614798690871375",
         "0.049815882391919475",
         "2001",
         "2"
        ],
        [
         "2001-09-30 00:00:00",
         "-0.2285423812538241",
         "-0.2043074259590717",
         "2001",
         "3"
        ],
        [
         "2001-12-31 00:00:00",
         "-0.06974715719535496",
         "-0.06737040121935323",
         "2001",
         "4"
        ],
        [
         "2002-03-31 00:00:00",
         "-0.025891712725157667",
         "-0.025559396586815608",
         "2002",
         "1"
        ],
        [
         "2002-06-30 00:00:00",
         "0.07727734961109967",
         "0.08034166718623359",
         "2002",
         "2"
        ],
        [
         "2002-09-30 00:00:00",
         "-0.09126587475561987",
         "-0.08722500519402532",
         "2002",
         "3"
        ],
        [
         "2002-12-31 00:00:00",
         "-0.1526943742575222",
         "-0.14160797157345006",
         "2002",
         "4"
        ],
        [
         "2003-03-31 00:00:00",
         "0.10673841907813575",
         "0.11264317018377357",
         "2003",
         "1"
        ],
        [
         "2003-06-30 00:00:00",
         "-0.016392277882089523",
         "-0.016258655615723705",
         "2003",
         "2"
        ],
        [
         "2003-09-30 00:00:00",
         "-0.08336580963931084",
         "-0.07998546452941413",
         "2003",
         "3"
        ],
        [
         "2003-12-31 00:00:00",
         "0.09075422967150804",
         "0.09499985371134279",
         "2003",
         "4"
        ],
        [
         "2004-03-31 00:00:00",
         "0.1513258893780236",
         "0.16337572810345802",
         "2004",
         "1"
        ],
        [
         "2004-06-30 00:00:00",
         "-0.21894365801623916",
         "-0.19663301983211068",
         "2004",
         "2"
        ],
        [
         "2004-09-30 00:00:00",
         "-0.0017597452195916574",
         "-0.0017581977758081013",
         "2004",
         "3"
        ],
        [
         "2004-12-31 00:00:00",
         "-0.09785512086999582",
         "-0.09321973222596125",
         "2004",
         "4"
        ],
        [
         "2005-03-31 00:00:00",
         "-0.06969245627807652",
         "-0.06731938412949079",
         "2005",
         "1"
        ],
        [
         "2005-06-30 00:00:00",
         "-0.0887337012276701",
         "-0.0849107717314006",
         "2005",
         "2"
        ],
        [
         "2005-09-30 00:00:00",
         "0.06679731013198165",
         "0.06907876477880315",
         "2005",
         "3"
        ],
        [
         "2005-12-31 00:00:00",
         "0.004705037881145735",
         "0.00471612395185228",
         "2005",
         "4"
        ],
        [
         "2006-03-31 00:00:00",
         "0.11172233541709709",
         "0.11820233235147226",
         "2006",
         "1"
        ],
        [
         "2006-06-30 00:00:00",
         "0.2530903884777844",
         "0.2879996919047987",
         "2006",
         "2"
        ],
        [
         "2006-09-30 00:00:00",
         "0.046851584950049485",
         "0.04796646354225875",
         "2006",
         "3"
        ],
        [
         "2006-12-31 00:00:00",
         "0.4231273760370433",
         "0.5267287522397595",
         "2006",
         "4"
        ],
        [
         "2007-03-31 00:00:00",
         "0.1740069207064252",
         "0.19006380187406302",
         "2007",
         "1"
        ],
        [
         "2007-06-30 00:00:00",
         "0.18230166534889847",
         "0.19997613050333274",
         "2007",
         "2"
        ]
       ],
       "shape": {
        "columns": 4,
        "rows": 123
       }
      },
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Log_return</th>\n",
       "      <th>Raw_Return</th>\n",
       "      <th>Year</th>\n",
       "      <th>Quarter</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Day</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>1995-03-31</th>\n",
       "      <td>-0.001467</td>\n",
       "      <td>-0.001466</td>\n",
       "      <td>1995</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1995-06-30</th>\n",
       "      <td>-0.025583</td>\n",
       "      <td>-0.025258</td>\n",
       "      <td>1995</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1995-09-30</th>\n",
       "      <td>0.135980</td>\n",
       "      <td>0.145660</td>\n",
       "      <td>1995</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1995-12-31</th>\n",
       "      <td>-0.263130</td>\n",
       "      <td>-0.231358</td>\n",
       "      <td>1995</td>\n",
       "      <td>4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1996-03-31</th>\n",
       "      <td>0.001979</td>\n",
       "      <td>0.001981</td>\n",
       "      <td>1996</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2024-09-30</th>\n",
       "      <td>0.117234</td>\n",
       "      <td>0.124383</td>\n",
       "      <td>2024</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2024-12-31</th>\n",
       "      <td>0.004565</td>\n",
       "      <td>0.004575</td>\n",
       "      <td>2024</td>\n",
       "      <td>4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2025-03-31</th>\n",
       "      <td>-0.004790</td>\n",
       "      <td>-0.004779</td>\n",
       "      <td>2025</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2025-06-30</th>\n",
       "      <td>0.032061</td>\n",
       "      <td>0.032580</td>\n",
       "      <td>2025</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2025-09-30</th>\n",
       "      <td>0.113373</td>\n",
       "      <td>0.120049</td>\n",
       "      <td>2025</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>123 rows × 4 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "            Log_return  Raw_Return  Year  Quarter\n",
       "Day                                              \n",
       "1995-03-31   -0.001467   -0.001466  1995        1\n",
       "1995-06-30   -0.025583   -0.025258  1995        2\n",
       "1995-09-30    0.135980    0.145660  1995        3\n",
       "1995-12-31   -0.263130   -0.231358  1995        4\n",
       "1996-03-31    0.001979    0.001981  1996        1\n",
       "...                ...         ...   ...      ...\n",
       "2024-09-30    0.117234    0.124383  2024        3\n",
       "2024-12-31    0.004565    0.004575  2024        4\n",
       "2025-03-31   -0.004790   -0.004779  2025        1\n",
       "2025-06-30    0.032061    0.032580  2025        2\n",
       "2025-09-30    0.113373    0.120049  2025        3\n",
       "\n",
       "[123 rows x 4 columns]"
      ]
     },
     "execution_count": 52,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "季度末价格计算的收益率:\n"
     ]
    },
    {
     "data": {
      "application/vnd.microsoft.datawrangler.viewer.v0+json": {
       "columns": [
        {
         "name": "Day",
         "rawType": "datetime64[ns]",
         "type": "datetime"
        },
        {
         "name": "Close",
         "rawType": "float64",
         "type": "float"
        },
        {
         "name": "Preclose",
         "rawType": "float64",
         "type": "float"
        },
        {
         "name": "Raw_return",
         "rawType": "float64",
         "type": "float"
        },
        {
         "name": "Log_return",
         "rawType": "float64",
         "type": "float"
        }
       ],
       "ref": "fe203d3d-0154-44c3-a648-253b7e81e995",
       "rows": [
        [
         "1995-03-31 00:00:00",
         "646.92",
         null,
         null,
         null
        ],
        [
         "1995-06-30 00:00:00",
         "630.58",
         "646.92",
         "-0.025258146293204553",
         "-0.02558260846977145"
        ],
        [
         "1995-09-30 00:00:00",
         "722.43",
         "630.58",
         "0.14565955152399357",
         "0.13598049869789897"
        ],
        [
         "1995-12-31 00:00:00",
         "555.29",
         "722.43",
         "-0.23135805545173926",
         "-0.2631300296781047"
        ],
        [
         "1996-03-31 00:00:00",
         "556.39",
         "555.29",
         "0.0019809468926146323",
         "0.0019789874046534095"
        ],
        [
         "1996-06-30 00:00:00",
         "804.25",
         "556.39",
         "0.4454788907061593",
         "0.368440678912882"
        ],
        [
         "1996-09-30 00:00:00",
         "875.53",
         "804.25",
         "0.0886291576002487",
         "0.08491925115333832"
        ],
        [
         "1996-12-31 00:00:00",
         "917.02",
         "875.53",
         "0.04738843900266132",
         "0.04629986499696859"
        ],
        [
         "1997-03-31 00:00:00",
         "1234.62",
         "917.02",
         "0.34633922924254645",
         "0.2973892271379519"
        ],
        [
         "1997-06-30 00:00:00",
         "1250.27",
         "1234.62",
         "0.012675965074274043",
         "0.012596297564311243"
        ],
        [
         "1997-09-30 00:00:00",
         "1097.38",
         "1250.27",
         "-0.12228558631335618",
         "-0.1304340074081125"
        ],
        [
         "1997-12-31 00:00:00",
         "1194.1",
         "1097.38",
         "0.08813719951156362",
         "0.08446724297574537"
        ],
        [
         "1998-03-31 00:00:00",
         "1243.02",
         "1194.1",
         "0.04096809312452909",
         "0.040151138946513676"
        ],
        [
         "1998-06-30 00:00:00",
         "1339.2",
         "1243.02",
         "0.07737606796350827",
         "0.07452851824935891"
        ],
        [
         "1998-09-30 00:00:00",
         "1242.9",
         "1339.2",
         "-0.07190860215053763",
         "-0.07462506198374541"
        ],
        [
         "1998-12-31 00:00:00",
         "1146.7",
         "1242.9",
         "-0.07739962989781968",
         "-0.08055910670720667"
        ],
        [
         "1999-03-31 00:00:00",
         "1158.05",
         "1146.7",
         "0.009897968082323017",
         "0.009849304049462049"
        ],
        [
         "1999-06-30 00:00:00",
         "1689.43",
         "1158.05",
         "0.4588575622814215",
         "0.37765363782583616"
        ],
        [
         "1999-09-30 00:00:00",
         "1570.7",
         "1689.43",
         "-0.07027814114819797",
         "-0.0728698140705708"
        ],
        [
         "1999-12-31 00:00:00",
         "1366.58",
         "1570.7",
         "-0.1299547972241677",
         "-0.13921011146963647"
        ],
        [
         "2000-03-31 00:00:00",
         "1800.22",
         "1366.58",
         "0.317317683560421",
         "0.2755976112586014"
        ],
        [
         "2000-06-30 00:00:00",
         "1928.11",
         "1800.22",
         "0.07104131717234563",
         "0.06863136884693422"
        ],
        [
         "2000-09-30 00:00:00",
         "1910.16",
         "1928.11",
         "-0.009309634823739255",
         "-0.009353240319182277"
        ],
        [
         "2000-12-31 00:00:00",
         "2073.48",
         "1910.16",
         "0.08550069104158808",
         "0.08204134694732712"
        ],
        [
         "2001-03-31 00:00:00",
         "2112.78",
         "2073.48",
         "0.018953643150645405",
         "0.018776260713766924"
        ],
        [
         "2001-06-30 00:00:00",
         "2218.03",
         "2112.78",
         "0.0498158823919197",
         "0.048614798690871375"
        ],
        [
         "2001-09-30 00:00:00",
         "1764.87",
         "2218.03",
         "-0.20430742595907192",
         "-0.2285423812538241"
        ],
        [
         "2001-12-31 00:00:00",
         "1645.97",
         "1764.87",
         "-0.06737040121935323",
         "-0.06974715719535496"
        ],
        [
         "2002-03-31 00:00:00",
         "1603.9",
         "1645.97",
         "-0.025559396586815053",
         "-0.025891712725157667"
        ],
        [
         "2002-06-30 00:00:00",
         "1732.76",
         "1603.9",
         "0.08034166718623359",
         "0.07727734961109967"
        ],
        [
         "2002-09-30 00:00:00",
         "1581.62",
         "1732.76",
         "-0.08722500519402576",
         "-0.09126587475561987"
        ],
        [
         "2002-12-31 00:00:00",
         "1357.65",
         "1581.62",
         "-0.14160797157344984",
         "-0.1526943742575222"
        ],
        [
         "2003-03-31 00:00:00",
         "1510.58",
         "1357.65",
         "0.11264317018377334",
         "0.10673841907813575"
        ],
        [
         "2003-06-30 00:00:00",
         "1486.02",
         "1510.58",
         "-0.016258655615723705",
         "-0.016392277882089523"
        ],
        [
         "2003-09-30 00:00:00",
         "1367.16",
         "1486.02",
         "-0.07998546452941402",
         "-0.08336580963931084"
        ],
        [
         "2003-12-31 00:00:00",
         "1497.04",
         "1367.16",
         "0.09499985371134323",
         "0.09075422967150804"
        ],
        [
         "2004-03-31 00:00:00",
         "1741.62",
         "1497.04",
         "0.16337572810345735",
         "0.1513258893780236"
        ],
        [
         "2004-06-30 00:00:00",
         "1399.16",
         "1741.62",
         "-0.19663301983211023",
         "-0.21894365801623916"
        ],
        [
         "2004-09-30 00:00:00",
         "1396.7",
         "1399.16",
         "-0.0017581977758083234",
         "-0.0017597452195916574"
        ],
        [
         "2004-12-31 00:00:00",
         "1266.5",
         "1396.7",
         "-0.09321973222596125",
         "-0.09785512086999582"
        ],
        [
         "2005-03-31 00:00:00",
         "1181.24",
         "1266.5",
         "-0.06731938412949068",
         "-0.06969245627807652"
        ],
        [
         "2005-06-30 00:00:00",
         "1080.94",
         "1181.24",
         "-0.08491077173140082",
         "-0.0887337012276701"
        ],
        [
         "2005-09-30 00:00:00",
         "1155.61",
         "1080.94",
         "0.06907876477880359",
         "0.06679731013198165"
        ],
        [
         "2005-12-31 00:00:00",
         "1161.06",
         "1155.61",
         "0.00471612395185228",
         "0.004705037881145735"
        ],
        [
         "2006-03-31 00:00:00",
         "1298.3",
         "1161.06",
         "0.11820233235147204",
         "0.11172233541709709"
        ],
        [
         "2006-06-30 00:00:00",
         "1672.21",
         "1298.3",
         "0.2879996919047987",
         "0.2530903884777844"
        ],
        [
         "2006-09-30 00:00:00",
         "1752.42",
         "1672.21",
         "0.04796646354225853",
         "0.046851584950049485"
        ],
        [
         "2006-12-31 00:00:00",
         "2675.47",
         "1752.42",
         "0.5267287522397597",
         "0.4231273760370433"
        ],
        [
         "2007-03-31 00:00:00",
         "3183.98",
         "2675.47",
         "0.19006380187406324",
         "0.1740069207064252"
        ],
        [
         "2007-06-30 00:00:00",
         "3820.7",
         "3183.98",
         "0.1999761305033323",
         "0.18230166534889847"
        ]
       ],
       "shape": {
        "columns": 4,
        "rows": 123
       }
      },
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Close</th>\n",
       "      <th>Preclose</th>\n",
       "      <th>Raw_return</th>\n",
       "      <th>Log_return</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Day</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>1995-03-31</th>\n",
       "      <td>646.920</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1995-06-30</th>\n",
       "      <td>630.580</td>\n",
       "      <td>646.9200</td>\n",
       "      <td>-0.025258</td>\n",
       "      <td>-0.025583</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1995-09-30</th>\n",
       "      <td>722.430</td>\n",
       "      <td>630.5800</td>\n",
       "      <td>0.145660</td>\n",
       "      <td>0.135980</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1995-12-31</th>\n",
       "      <td>555.290</td>\n",
       "      <td>722.4300</td>\n",
       "      <td>-0.231358</td>\n",
       "      <td>-0.263130</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1996-03-31</th>\n",
       "      <td>556.390</td>\n",
       "      <td>555.2900</td>\n",
       "      <td>0.001981</td>\n",
       "      <td>0.001979</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2024-09-30</th>\n",
       "      <td>3336.497</td>\n",
       "      <td>2967.4028</td>\n",
       "      <td>0.124383</td>\n",
       "      <td>0.117234</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2024-12-31</th>\n",
       "      <td>3351.763</td>\n",
       "      <td>3336.4970</td>\n",
       "      <td>0.004575</td>\n",
       "      <td>0.004565</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2025-03-31</th>\n",
       "      <td>3335.746</td>\n",
       "      <td>3351.7630</td>\n",
       "      <td>-0.004779</td>\n",
       "      <td>-0.004790</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2025-06-30</th>\n",
       "      <td>3444.426</td>\n",
       "      <td>3335.7460</td>\n",
       "      <td>0.032580</td>\n",
       "      <td>0.032061</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2025-09-30</th>\n",
       "      <td>3857.927</td>\n",
       "      <td>3444.4260</td>\n",
       "      <td>0.120049</td>\n",
       "      <td>0.113373</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>123 rows × 4 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "               Close   Preclose  Raw_return  Log_return\n",
       "Day                                                    \n",
       "1995-03-31   646.920        NaN         NaN         NaN\n",
       "1995-06-30   630.580   646.9200   -0.025258   -0.025583\n",
       "1995-09-30   722.430   630.5800    0.145660    0.135980\n",
       "1995-12-31   555.290   722.4300   -0.231358   -0.263130\n",
       "1996-03-31   556.390   555.2900    0.001981    0.001979\n",
       "...              ...        ...         ...         ...\n",
       "2024-09-30  3336.497  2967.4028    0.124383    0.117234\n",
       "2024-12-31  3351.763  3336.4970    0.004575    0.004565\n",
       "2025-03-31  3335.746  3351.7630   -0.004779   -0.004790\n",
       "2025-06-30  3444.426  3335.7460    0.032580    0.032061\n",
       "2025-09-30  3857.927  3444.4260    0.120049    0.113373\n",
       "\n",
       "[123 rows x 4 columns]"
      ]
     },
     "execution_count": 52,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 计算季度收益率\n",
    "# 方法1：使用resample函数的'QE'参数（季度末）\n",
    "Quarter_data1 = data_new.resample('QE')['Log_return'].sum().to_frame(name='Log_return')\n",
    "Quarter_data1['Raw_Return'] = np.exp(Quarter_data1['Log_return']) - 1\n",
    "Quarter_data1['Year'] = Quarter_data1.index.year\n",
    "Quarter_data1['Quarter'] = Quarter_data1.index.quarter\n",
    "\n",
    "# 方法2：使用季度末价格计算\n",
    "Quarter_data2 = data_new.resample('QE')['Close'].last().to_frame()\n",
    "Quarter_data2['Preclose'] = Quarter_data2['Close'].shift(1)\n",
    "Quarter_data2['Raw_return'] = Quarter_data2['Close'] / Quarter_data2['Preclose'] - 1\n",
    "Quarter_data2['Log_return'] = np.log(Quarter_data2['Close']) - np.log(Quarter_data2['Preclose'])\n",
    "\n",
    "# 显示结果\n",
    "print(\"季度对数收益率汇总:\")\n",
    "Quarter_data1\n",
    "print(\"\\n季度末价格计算的收益率:\")\n",
    "Quarter_data2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "id": "263379dd",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "年度对数收益率汇总:\n"
     ]
    },
    {
     "data": {
      "application/vnd.microsoft.datawrangler.viewer.v0+json": {
       "columns": [
        {
         "name": "Day",
         "rawType": "datetime64[ns]",
         "type": "datetime"
        },
        {
         "name": "Log_return",
         "rawType": "float64",
         "type": "float"
        },
        {
         "name": "Raw_Return",
         "rawType": "float64",
         "type": "float"
        }
       ],
       "ref": "440899b5-9c71-483c-a687-d6e8f2d84e1d",
       "rows": [
        [
         "1995-12-31 00:00:00",
         "-0.15419955914038486",
         "-0.14289903838733065"
        ],
        [
         "1996-12-31 00:00:00",
         "0.5016387824678423",
         "0.6514253813322761"
        ],
        [
         "1997-12-31 00:00:00",
         "0.264018760269896",
         "0.3021526248064379"
        ],
        [
         "1998-12-31 00:00:00",
         "-0.0405045114950795",
         "-0.03969516790888461"
        ],
        [
         "1999-12-31 00:00:00",
         "0.17542301633509094",
         "0.19175023981860928"
        ],
        [
         "2000-12-31 00:00:00",
         "0.41691708673368044",
         "0.5172767053520466"
        ],
        [
         "2001-12-31 00:00:00",
         "-0.23089847904454075",
         "-0.20617994868530154"
        ],
        [
         "2002-12-31 00:00:00",
         "-0.19257461212720006",
         "-0.17516722662016937"
        ],
        [
         "2003-12-31 00:00:00",
         "0.09773456122824342",
         "0.10267005487423098"
        ],
        [
         "2004-12-31 00:00:00",
         "-0.16723263472780303",
         "-0.15399722118313464"
        ],
        [
         "2005-12-31 00:00:00",
         "-0.08692380949261924",
         "-0.08325305961310725"
        ],
        [
         "2006-12-31 00:00:00",
         "0.8347916848819743",
         "1.3043339706819639"
        ],
        [
         "2007-12-31 00:00:00",
         "0.676302494876893",
         "0.9665927855666481"
        ],
        [
         "2008-12-31 00:00:00",
         "-1.0611461036996594",
         "-0.6539410364986811"
        ],
        [
         "2009-12-31 00:00:00",
         "0.5876896337797675",
         "0.7998253524530279"
        ],
        [
         "2010-12-31 00:00:00",
         "-0.15447011501972785",
         "-0.1431309007244118"
        ],
        [
         "2011-12-31 00:00:00",
         "-0.2443072865437621",
         "-0.2167530839577213"
        ],
        [
         "2012-12-31 00:00:00",
         "0.031202808876106936",
         "0.03169471951696279"
        ],
        [
         "2013-12-31 00:00:00",
         "-0.06987843596302312",
         "-0.06749282764760078"
        ],
        [
         "2014-12-31 00:00:00",
         "0.42441194282114303",
         "0.5286911974593345"
        ],
        [
         "2015-12-31 00:00:00",
         "0.08996505691859547",
         "0.09413605055214003"
        ],
        [
         "2016-12-31 00:00:00",
         "-0.13131944568188025",
         "-0.12306240428573745"
        ],
        [
         "2017-12-31 00:00:00",
         "0.06351722257326031",
         "0.06557783763580671"
        ],
        [
         "2018-12-31 00:00:00",
         "-0.2822450885636467",
         "-0.24591115666869223"
        ],
        [
         "2019-12-31 00:00:00",
         "0.2013331838846355",
         "0.22303219856449785"
        ],
        [
         "2020-12-31 00:00:00",
         "0.12985799493633898",
         "0.13866667540949207"
        ],
        [
         "2021-12-31 00:00:00",
         "0.046884311217676",
         "0.048000760134404796"
        ],
        [
         "2022-12-31 00:00:00",
         "-0.16399106052557322",
         "-0.1512503907782804"
        ],
        [
         "2023-12-31 00:00:00",
         "-0.03770877746284196",
         "-0.037006654575521214"
        ],
        [
         "2024-12-31 00:00:00",
         "0.11926435327186002",
         "0.12666771722190417"
        ],
        [
         "2025-12-31 00:00:00",
         "0.14064351652051243",
         "0.15101425727296336"
        ]
       ],
       "shape": {
        "columns": 2,
        "rows": 31
       }
      },
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Log_return</th>\n",
       "      <th>Raw_Return</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Day</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>1995-12-31</th>\n",
       "      <td>-0.154200</td>\n",
       "      <td>-0.142899</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1996-12-31</th>\n",
       "      <td>0.501639</td>\n",
       "      <td>0.651425</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1997-12-31</th>\n",
       "      <td>0.264019</td>\n",
       "      <td>0.302153</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1998-12-31</th>\n",
       "      <td>-0.040505</td>\n",
       "      <td>-0.039695</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1999-12-31</th>\n",
       "      <td>0.175423</td>\n",
       "      <td>0.191750</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2000-12-31</th>\n",
       "      <td>0.416917</td>\n",
       "      <td>0.517277</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2001-12-31</th>\n",
       "      <td>-0.230898</td>\n",
       "      <td>-0.206180</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2002-12-31</th>\n",
       "      <td>-0.192575</td>\n",
       "      <td>-0.175167</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2003-12-31</th>\n",
       "      <td>0.097735</td>\n",
       "      <td>0.102670</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2004-12-31</th>\n",
       "      <td>-0.167233</td>\n",
       "      <td>-0.153997</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2005-12-31</th>\n",
       "      <td>-0.086924</td>\n",
       "      <td>-0.083253</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2006-12-31</th>\n",
       "      <td>0.834792</td>\n",
       "      <td>1.304334</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2007-12-31</th>\n",
       "      <td>0.676302</td>\n",
       "      <td>0.966593</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2008-12-31</th>\n",
       "      <td>-1.061146</td>\n",
       "      <td>-0.653941</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2009-12-31</th>\n",
       "      <td>0.587690</td>\n",
       "      <td>0.799825</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2010-12-31</th>\n",
       "      <td>-0.154470</td>\n",
       "      <td>-0.143131</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2011-12-31</th>\n",
       "      <td>-0.244307</td>\n",
       "      <td>-0.216753</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2012-12-31</th>\n",
       "      <td>0.031203</td>\n",
       "      <td>0.031695</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-12-31</th>\n",
       "      <td>-0.069878</td>\n",
       "      <td>-0.067493</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2014-12-31</th>\n",
       "      <td>0.424412</td>\n",
       "      <td>0.528691</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2015-12-31</th>\n",
       "      <td>0.089965</td>\n",
       "      <td>0.094136</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2016-12-31</th>\n",
       "      <td>-0.131319</td>\n",
       "      <td>-0.123062</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2017-12-31</th>\n",
       "      <td>0.063517</td>\n",
       "      <td>0.065578</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2018-12-31</th>\n",
       "      <td>-0.282245</td>\n",
       "      <td>-0.245911</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2019-12-31</th>\n",
       "      <td>0.201333</td>\n",
       "      <td>0.223032</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2020-12-31</th>\n",
       "      <td>0.129858</td>\n",
       "      <td>0.138667</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2021-12-31</th>\n",
       "      <td>0.046884</td>\n",
       "      <td>0.048001</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2022-12-31</th>\n",
       "      <td>-0.163991</td>\n",
       "      <td>-0.151250</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2023-12-31</th>\n",
       "      <td>-0.037709</td>\n",
       "      <td>-0.037007</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2024-12-31</th>\n",
       "      <td>0.119264</td>\n",
       "      <td>0.126668</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2025-12-31</th>\n",
       "      <td>0.140644</td>\n",
       "      <td>0.151014</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "            Log_return  Raw_Return\n",
       "Day                               \n",
       "1995-12-31   -0.154200   -0.142899\n",
       "1996-12-31    0.501639    0.651425\n",
       "1997-12-31    0.264019    0.302153\n",
       "1998-12-31   -0.040505   -0.039695\n",
       "1999-12-31    0.175423    0.191750\n",
       "2000-12-31    0.416917    0.517277\n",
       "2001-12-31   -0.230898   -0.206180\n",
       "2002-12-31   -0.192575   -0.175167\n",
       "2003-12-31    0.097735    0.102670\n",
       "2004-12-31   -0.167233   -0.153997\n",
       "2005-12-31   -0.086924   -0.083253\n",
       "2006-12-31    0.834792    1.304334\n",
       "2007-12-31    0.676302    0.966593\n",
       "2008-12-31   -1.061146   -0.653941\n",
       "2009-12-31    0.587690    0.799825\n",
       "2010-12-31   -0.154470   -0.143131\n",
       "2011-12-31   -0.244307   -0.216753\n",
       "2012-12-31    0.031203    0.031695\n",
       "2013-12-31   -0.069878   -0.067493\n",
       "2014-12-31    0.424412    0.528691\n",
       "2015-12-31    0.089965    0.094136\n",
       "2016-12-31   -0.131319   -0.123062\n",
       "2017-12-31    0.063517    0.065578\n",
       "2018-12-31   -0.282245   -0.245911\n",
       "2019-12-31    0.201333    0.223032\n",
       "2020-12-31    0.129858    0.138667\n",
       "2021-12-31    0.046884    0.048001\n",
       "2022-12-31   -0.163991   -0.151250\n",
       "2023-12-31   -0.037709   -0.037007\n",
       "2024-12-31    0.119264    0.126668\n",
       "2025-12-31    0.140644    0.151014"
      ]
     },
     "execution_count": 53,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "年末价格计算的收益率:\n"
     ]
    },
    {
     "data": {
      "application/vnd.microsoft.datawrangler.viewer.v0+json": {
       "columns": [
        {
         "name": "Day",
         "rawType": "datetime64[ns]",
         "type": "datetime"
        },
        {
         "name": "Close",
         "rawType": "float64",
         "type": "float"
        },
        {
         "name": "Preclose",
         "rawType": "float64",
         "type": "float"
        },
        {
         "name": "Raw_return",
         "rawType": "float64",
         "type": "float"
        },
        {
         "name": "Log_return",
         "rawType": "float64",
         "type": "float"
        }
       ],
       "ref": "e224902a-6008-45d2-96de-a1e3684da412",
       "rows": [
        [
         "1995-12-31 00:00:00",
         "555.29",
         null,
         null,
         null
        ],
        [
         "1996-12-31 00:00:00",
         "917.02",
         "555.29",
         "0.651425381332277",
         "0.5016387824678423"
        ],
        [
         "1997-12-31 00:00:00",
         "1194.1",
         "917.02",
         "0.3021526248064381",
         "0.264018760269896"
        ],
        [
         "1998-12-31 00:00:00",
         "1146.7",
         "1194.1",
         "-0.039695167908885276",
         "-0.0405045114950795"
        ],
        [
         "1999-12-31 00:00:00",
         "1366.58",
         "1146.7",
         "0.19175023981860972",
         "0.17542301633509094"
        ],
        [
         "2000-12-31 00:00:00",
         "2073.48",
         "1366.58",
         "0.5172767053520468",
         "0.41691708673368044"
        ],
        [
         "2001-12-31 00:00:00",
         "1645.97",
         "2073.48",
         "-0.20617994868530198",
         "-0.23089847904454075"
        ],
        [
         "2002-12-31 00:00:00",
         "1357.65",
         "1645.97",
         "-0.17516722662016926",
         "-0.19257461212720006"
        ],
        [
         "2003-12-31 00:00:00",
         "1497.04",
         "1357.65",
         "0.1026700548742312",
         "0.09773456122824342"
        ],
        [
         "2004-12-31 00:00:00",
         "1266.5",
         "1497.04",
         "-0.15399722118313475",
         "-0.16723263472780303"
        ],
        [
         "2005-12-31 00:00:00",
         "1161.06",
         "1266.5",
         "-0.08325305961310703",
         "-0.08692380949261924"
        ],
        [
         "2006-12-31 00:00:00",
         "2675.47",
         "1161.06",
         "1.304333970681963",
         "0.8347916848819743"
        ],
        [
         "2007-12-31 00:00:00",
         "5261.56",
         "2675.47",
         "0.9665927855666483",
         "0.676302494876893"
        ],
        [
         "2008-12-31 00:00:00",
         "1820.81",
         "5261.56",
         "-0.6539410364986811",
         "-1.0611461036996594"
        ],
        [
         "2009-12-31 00:00:00",
         "3277.14",
         "1820.81",
         "0.7998253524530292",
         "0.5876896337797675"
        ],
        [
         "2010-12-31 00:00:00",
         "2808.08",
         "3277.14",
         "-0.14313090072441215",
         "-0.15447011501972785"
        ],
        [
         "2011-12-31 00:00:00",
         "2199.42",
         "2808.08",
         "-0.21675308395772197",
         "-0.2443072865437621"
        ],
        [
         "2012-12-31 00:00:00",
         "2269.13",
         "2199.42",
         "0.031694719516963676",
         "0.031202808876106936"
        ],
        [
         "2013-12-31 00:00:00",
         "2115.98",
         "2269.13",
         "-0.06749282764760067",
         "-0.06987843596302312"
        ],
        [
         "2014-12-31 00:00:00",
         "3234.68",
         "2115.98",
         "0.5286911974593331",
         "0.42441194282114303"
        ],
        [
         "2015-12-31 00:00:00",
         "3539.18",
         "3234.68",
         "0.09413605055214114",
         "0.08996505691859547"
        ],
        [
         "2016-12-31 00:00:00",
         "3103.64",
         "3539.18",
         "-0.12306240428573845",
         "-0.13131944568188025"
        ],
        [
         "2017-12-31 00:00:00",
         "3307.17",
         "3103.64",
         "0.06557783763580827",
         "0.06351722257326031"
        ],
        [
         "2018-12-31 00:00:00",
         "2493.9",
         "3307.17",
         "-0.24591115666869257",
         "-0.2822450885636467"
        ],
        [
         "2019-12-31 00:00:00",
         "3050.12",
         "2493.9",
         "0.22303219856449719",
         "0.2013331838846355"
        ],
        [
         "2020-12-31 00:00:00",
         "3473.07",
         "3050.12",
         "0.1386666754094923",
         "0.12985799493633898"
        ],
        [
         "2021-12-31 00:00:00",
         "3639.78",
         "3473.07",
         "0.048000760134405684",
         "0.046884311217676"
        ],
        [
         "2022-12-31 00:00:00",
         "3089.2579",
         "3639.78",
         "-0.15125147673760508",
         "-0.16399234000777518"
        ],
        [
         "2023-12-31 00:00:00",
         "2974.9348",
         "3089.2579",
         "-0.037006654575521214",
         "-0.03770877746284196"
        ],
        [
         "2024-12-31 00:00:00",
         "3351.763",
         "2974.9348",
         "0.1266677172219035",
         "0.11926435327186002"
        ],
        [
         "2025-12-31 00:00:00",
         "3857.927",
         "3351.763",
         "0.15101425727296358",
         "0.14064351652051243"
        ]
       ],
       "shape": {
        "columns": 4,
        "rows": 31
       }
      },
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Close</th>\n",
       "      <th>Preclose</th>\n",
       "      <th>Raw_return</th>\n",
       "      <th>Log_return</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Day</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>1995-12-31</th>\n",
       "      <td>555.2900</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1996-12-31</th>\n",
       "      <td>917.0200</td>\n",
       "      <td>555.2900</td>\n",
       "      <td>0.651425</td>\n",
       "      <td>0.501639</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1997-12-31</th>\n",
       "      <td>1194.1000</td>\n",
       "      <td>917.0200</td>\n",
       "      <td>0.302153</td>\n",
       "      <td>0.264019</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1998-12-31</th>\n",
       "      <td>1146.7000</td>\n",
       "      <td>1194.1000</td>\n",
       "      <td>-0.039695</td>\n",
       "      <td>-0.040505</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1999-12-31</th>\n",
       "      <td>1366.5800</td>\n",
       "      <td>1146.7000</td>\n",
       "      <td>0.191750</td>\n",
       "      <td>0.175423</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2000-12-31</th>\n",
       "      <td>2073.4800</td>\n",
       "      <td>1366.5800</td>\n",
       "      <td>0.517277</td>\n",
       "      <td>0.416917</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2001-12-31</th>\n",
       "      <td>1645.9700</td>\n",
       "      <td>2073.4800</td>\n",
       "      <td>-0.206180</td>\n",
       "      <td>-0.230898</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2002-12-31</th>\n",
       "      <td>1357.6500</td>\n",
       "      <td>1645.9700</td>\n",
       "      <td>-0.175167</td>\n",
       "      <td>-0.192575</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2003-12-31</th>\n",
       "      <td>1497.0400</td>\n",
       "      <td>1357.6500</td>\n",
       "      <td>0.102670</td>\n",
       "      <td>0.097735</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2004-12-31</th>\n",
       "      <td>1266.5000</td>\n",
       "      <td>1497.0400</td>\n",
       "      <td>-0.153997</td>\n",
       "      <td>-0.167233</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2005-12-31</th>\n",
       "      <td>1161.0600</td>\n",
       "      <td>1266.5000</td>\n",
       "      <td>-0.083253</td>\n",
       "      <td>-0.086924</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2006-12-31</th>\n",
       "      <td>2675.4700</td>\n",
       "      <td>1161.0600</td>\n",
       "      <td>1.304334</td>\n",
       "      <td>0.834792</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2007-12-31</th>\n",
       "      <td>5261.5600</td>\n",
       "      <td>2675.4700</td>\n",
       "      <td>0.966593</td>\n",
       "      <td>0.676302</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2008-12-31</th>\n",
       "      <td>1820.8100</td>\n",
       "      <td>5261.5600</td>\n",
       "      <td>-0.653941</td>\n",
       "      <td>-1.061146</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2009-12-31</th>\n",
       "      <td>3277.1400</td>\n",
       "      <td>1820.8100</td>\n",
       "      <td>0.799825</td>\n",
       "      <td>0.587690</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2010-12-31</th>\n",
       "      <td>2808.0800</td>\n",
       "      <td>3277.1400</td>\n",
       "      <td>-0.143131</td>\n",
       "      <td>-0.154470</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2011-12-31</th>\n",
       "      <td>2199.4200</td>\n",
       "      <td>2808.0800</td>\n",
       "      <td>-0.216753</td>\n",
       "      <td>-0.244307</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2012-12-31</th>\n",
       "      <td>2269.1300</td>\n",
       "      <td>2199.4200</td>\n",
       "      <td>0.031695</td>\n",
       "      <td>0.031203</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013-12-31</th>\n",
       "      <td>2115.9800</td>\n",
       "      <td>2269.1300</td>\n",
       "      <td>-0.067493</td>\n",
       "      <td>-0.069878</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2014-12-31</th>\n",
       "      <td>3234.6800</td>\n",
       "      <td>2115.9800</td>\n",
       "      <td>0.528691</td>\n",
       "      <td>0.424412</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2015-12-31</th>\n",
       "      <td>3539.1800</td>\n",
       "      <td>3234.6800</td>\n",
       "      <td>0.094136</td>\n",
       "      <td>0.089965</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2016-12-31</th>\n",
       "      <td>3103.6400</td>\n",
       "      <td>3539.1800</td>\n",
       "      <td>-0.123062</td>\n",
       "      <td>-0.131319</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2017-12-31</th>\n",
       "      <td>3307.1700</td>\n",
       "      <td>3103.6400</td>\n",
       "      <td>0.065578</td>\n",
       "      <td>0.063517</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2018-12-31</th>\n",
       "      <td>2493.9000</td>\n",
       "      <td>3307.1700</td>\n",
       "      <td>-0.245911</td>\n",
       "      <td>-0.282245</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2019-12-31</th>\n",
       "      <td>3050.1200</td>\n",
       "      <td>2493.9000</td>\n",
       "      <td>0.223032</td>\n",
       "      <td>0.201333</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2020-12-31</th>\n",
       "      <td>3473.0700</td>\n",
       "      <td>3050.1200</td>\n",
       "      <td>0.138667</td>\n",
       "      <td>0.129858</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2021-12-31</th>\n",
       "      <td>3639.7800</td>\n",
       "      <td>3473.0700</td>\n",
       "      <td>0.048001</td>\n",
       "      <td>0.046884</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2022-12-31</th>\n",
       "      <td>3089.2579</td>\n",
       "      <td>3639.7800</td>\n",
       "      <td>-0.151251</td>\n",
       "      <td>-0.163992</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2023-12-31</th>\n",
       "      <td>2974.9348</td>\n",
       "      <td>3089.2579</td>\n",
       "      <td>-0.037007</td>\n",
       "      <td>-0.037709</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2024-12-31</th>\n",
       "      <td>3351.7630</td>\n",
       "      <td>2974.9348</td>\n",
       "      <td>0.126668</td>\n",
       "      <td>0.119264</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2025-12-31</th>\n",
       "      <td>3857.9270</td>\n",
       "      <td>3351.7630</td>\n",
       "      <td>0.151014</td>\n",
       "      <td>0.140644</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                Close   Preclose  Raw_return  Log_return\n",
       "Day                                                     \n",
       "1995-12-31   555.2900        NaN         NaN         NaN\n",
       "1996-12-31   917.0200   555.2900    0.651425    0.501639\n",
       "1997-12-31  1194.1000   917.0200    0.302153    0.264019\n",
       "1998-12-31  1146.7000  1194.1000   -0.039695   -0.040505\n",
       "1999-12-31  1366.5800  1146.7000    0.191750    0.175423\n",
       "2000-12-31  2073.4800  1366.5800    0.517277    0.416917\n",
       "2001-12-31  1645.9700  2073.4800   -0.206180   -0.230898\n",
       "2002-12-31  1357.6500  1645.9700   -0.175167   -0.192575\n",
       "2003-12-31  1497.0400  1357.6500    0.102670    0.097735\n",
       "2004-12-31  1266.5000  1497.0400   -0.153997   -0.167233\n",
       "2005-12-31  1161.0600  1266.5000   -0.083253   -0.086924\n",
       "2006-12-31  2675.4700  1161.0600    1.304334    0.834792\n",
       "2007-12-31  5261.5600  2675.4700    0.966593    0.676302\n",
       "2008-12-31  1820.8100  5261.5600   -0.653941   -1.061146\n",
       "2009-12-31  3277.1400  1820.8100    0.799825    0.587690\n",
       "2010-12-31  2808.0800  3277.1400   -0.143131   -0.154470\n",
       "2011-12-31  2199.4200  2808.0800   -0.216753   -0.244307\n",
       "2012-12-31  2269.1300  2199.4200    0.031695    0.031203\n",
       "2013-12-31  2115.9800  2269.1300   -0.067493   -0.069878\n",
       "2014-12-31  3234.6800  2115.9800    0.528691    0.424412\n",
       "2015-12-31  3539.1800  3234.6800    0.094136    0.089965\n",
       "2016-12-31  3103.6400  3539.1800   -0.123062   -0.131319\n",
       "2017-12-31  3307.1700  3103.6400    0.065578    0.063517\n",
       "2018-12-31  2493.9000  3307.1700   -0.245911   -0.282245\n",
       "2019-12-31  3050.1200  2493.9000    0.223032    0.201333\n",
       "2020-12-31  3473.0700  3050.1200    0.138667    0.129858\n",
       "2021-12-31  3639.7800  3473.0700    0.048001    0.046884\n",
       "2022-12-31  3089.2579  3639.7800   -0.151251   -0.163992\n",
       "2023-12-31  2974.9348  3089.2579   -0.037007   -0.037709\n",
       "2024-12-31  3351.7630  2974.9348    0.126668    0.119264\n",
       "2025-12-31  3857.9270  3351.7630    0.151014    0.140644"
      ]
     },
     "execution_count": 53,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "使用groupby计算的年度收益率:\n"
     ]
    },
    {
     "data": {
      "application/vnd.microsoft.datawrangler.viewer.v0+json": {
       "columns": [
        {
         "name": "year",
         "rawType": "int32",
         "type": "integer"
        },
        {
         "name": "Log_return",
         "rawType": "float64",
         "type": "float"
        },
        {
         "name": "Raw_Return",
         "rawType": "float64",
         "type": "float"
        }
       ],
       "ref": "f9005e45-b70b-4c4c-8edb-1e2d8cc96b6c",
       "rows": [
        [
         "1995",
         "-0.15419955914038486",
         "-0.14289903838733065"
        ],
        [
         "1996",
         "0.5016387824678423",
         "0.6514253813322761"
        ],
        [
         "1997",
         "0.264018760269896",
         "0.3021526248064379"
        ],
        [
         "1998",
         "-0.0405045114950795",
         "-0.03969516790888461"
        ],
        [
         "1999",
         "0.17542301633509094",
         "0.19175023981860928"
        ],
        [
         "2000",
         "0.41691708673368044",
         "0.5172767053520466"
        ],
        [
         "2001",
         "-0.23089847904454075",
         "-0.20617994868530154"
        ],
        [
         "2002",
         "-0.19257461212720006",
         "-0.17516722662016937"
        ],
        [
         "2003",
         "0.09773456122824342",
         "0.10267005487423098"
        ],
        [
         "2004",
         "-0.16723263472780303",
         "-0.15399722118313464"
        ],
        [
         "2005",
         "-0.08692380949261924",
         "-0.08325305961310725"
        ],
        [
         "2006",
         "0.8347916848819743",
         "1.3043339706819639"
        ],
        [
         "2007",
         "0.676302494876893",
         "0.9665927855666481"
        ],
        [
         "2008",
         "-1.0611461036996594",
         "-0.6539410364986811"
        ],
        [
         "2009",
         "0.5876896337797675",
         "0.7998253524530279"
        ],
        [
         "2010",
         "-0.15447011501972785",
         "-0.1431309007244118"
        ],
        [
         "2011",
         "-0.2443072865437621",
         "-0.2167530839577213"
        ],
        [
         "2012",
         "0.031202808876106936",
         "0.03169471951696279"
        ],
        [
         "2013",
         "-0.06987843596302312",
         "-0.06749282764760078"
        ],
        [
         "2014",
         "0.42441194282114303",
         "0.5286911974593345"
        ],
        [
         "2015",
         "0.08996505691859547",
         "0.09413605055214003"
        ],
        [
         "2016",
         "-0.13131944568188025",
         "-0.12306240428573745"
        ],
        [
         "2017",
         "0.06351722257326031",
         "0.06557783763580671"
        ],
        [
         "2018",
         "-0.2822450885636467",
         "-0.24591115666869223"
        ],
        [
         "2019",
         "0.2013331838846355",
         "0.22303219856449785"
        ],
        [
         "2020",
         "0.12985799493633898",
         "0.13866667540949207"
        ],
        [
         "2021",
         "0.046884311217676",
         "0.048000760134404796"
        ],
        [
         "2022",
         "-0.16399106052557322",
         "-0.1512503907782804"
        ],
        [
         "2023",
         "-0.03770877746284196",
         "-0.037006654575521214"
        ],
        [
         "2024",
         "0.11926435327186002",
         "0.12666771722190417"
        ],
        [
         "2025",
         "0.14064351652051243",
         "0.15101425727296336"
        ]
       ],
       "shape": {
        "columns": 2,
        "rows": 31
       }
      },
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Log_return</th>\n",
       "      <th>Raw_Return</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>year</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>1995</th>\n",
       "      <td>-0.154200</td>\n",
       "      <td>-0.142899</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1996</th>\n",
       "      <td>0.501639</td>\n",
       "      <td>0.651425</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1997</th>\n",
       "      <td>0.264019</td>\n",
       "      <td>0.302153</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1998</th>\n",
       "      <td>-0.040505</td>\n",
       "      <td>-0.039695</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1999</th>\n",
       "      <td>0.175423</td>\n",
       "      <td>0.191750</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2000</th>\n",
       "      <td>0.416917</td>\n",
       "      <td>0.517277</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2001</th>\n",
       "      <td>-0.230898</td>\n",
       "      <td>-0.206180</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2002</th>\n",
       "      <td>-0.192575</td>\n",
       "      <td>-0.175167</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2003</th>\n",
       "      <td>0.097735</td>\n",
       "      <td>0.102670</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2004</th>\n",
       "      <td>-0.167233</td>\n",
       "      <td>-0.153997</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2005</th>\n",
       "      <td>-0.086924</td>\n",
       "      <td>-0.083253</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2006</th>\n",
       "      <td>0.834792</td>\n",
       "      <td>1.304334</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2007</th>\n",
       "      <td>0.676302</td>\n",
       "      <td>0.966593</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2008</th>\n",
       "      <td>-1.061146</td>\n",
       "      <td>-0.653941</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2009</th>\n",
       "      <td>0.587690</td>\n",
       "      <td>0.799825</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2010</th>\n",
       "      <td>-0.154470</td>\n",
       "      <td>-0.143131</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2011</th>\n",
       "      <td>-0.244307</td>\n",
       "      <td>-0.216753</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2012</th>\n",
       "      <td>0.031203</td>\n",
       "      <td>0.031695</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2013</th>\n",
       "      <td>-0.069878</td>\n",
       "      <td>-0.067493</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2014</th>\n",
       "      <td>0.424412</td>\n",
       "      <td>0.528691</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2015</th>\n",
       "      <td>0.089965</td>\n",
       "      <td>0.094136</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2016</th>\n",
       "      <td>-0.131319</td>\n",
       "      <td>-0.123062</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2017</th>\n",
       "      <td>0.063517</td>\n",
       "      <td>0.065578</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2018</th>\n",
       "      <td>-0.282245</td>\n",
       "      <td>-0.245911</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2019</th>\n",
       "      <td>0.201333</td>\n",
       "      <td>0.223032</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2020</th>\n",
       "      <td>0.129858</td>\n",
       "      <td>0.138667</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2021</th>\n",
       "      <td>0.046884</td>\n",
       "      <td>0.048001</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2022</th>\n",
       "      <td>-0.163991</td>\n",
       "      <td>-0.151250</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2023</th>\n",
       "      <td>-0.037709</td>\n",
       "      <td>-0.037007</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2024</th>\n",
       "      <td>0.119264</td>\n",
       "      <td>0.126668</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2025</th>\n",
       "      <td>0.140644</td>\n",
       "      <td>0.151014</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "      Log_return  Raw_Return\n",
       "year                        \n",
       "1995   -0.154200   -0.142899\n",
       "1996    0.501639    0.651425\n",
       "1997    0.264019    0.302153\n",
       "1998   -0.040505   -0.039695\n",
       "1999    0.175423    0.191750\n",
       "2000    0.416917    0.517277\n",
       "2001   -0.230898   -0.206180\n",
       "2002   -0.192575   -0.175167\n",
       "2003    0.097735    0.102670\n",
       "2004   -0.167233   -0.153997\n",
       "2005   -0.086924   -0.083253\n",
       "2006    0.834792    1.304334\n",
       "2007    0.676302    0.966593\n",
       "2008   -1.061146   -0.653941\n",
       "2009    0.587690    0.799825\n",
       "2010   -0.154470   -0.143131\n",
       "2011   -0.244307   -0.216753\n",
       "2012    0.031203    0.031695\n",
       "2013   -0.069878   -0.067493\n",
       "2014    0.424412    0.528691\n",
       "2015    0.089965    0.094136\n",
       "2016   -0.131319   -0.123062\n",
       "2017    0.063517    0.065578\n",
       "2018   -0.282245   -0.245911\n",
       "2019    0.201333    0.223032\n",
       "2020    0.129858    0.138667\n",
       "2021    0.046884    0.048001\n",
       "2022   -0.163991   -0.151250\n",
       "2023   -0.037709   -0.037007\n",
       "2024    0.119264    0.126668\n",
       "2025    0.140644    0.151014"
      ]
     },
     "execution_count": 53,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 计算年度收益率\n",
    "# 方法1：使用resample函数的'YE'参数（年末）\n",
    "Year_data1 = data_new.resample('YE')['Log_return'].sum().to_frame(name='Log_return')\n",
    "Year_data1['Raw_Return'] = np.exp(Year_data1['Log_return']) - 1\n",
    "\n",
    "# 方法2：使用年末价格计算\n",
    "Year_data2 = data_new.resample('YE')['Close'].last().to_frame()\n",
    "Year_data2['Preclose'] = Year_data2['Close'].shift(1)\n",
    "Year_data2['Raw_return'] = Year_data2['Close'] / Year_data2['Preclose'] - 1\n",
    "Year_data2['Log_return'] = np.log(Year_data2['Close']) - np.log(Year_data2['Preclose'])\n",
    "\n",
    "# 方法3：使用groupby按年分组\n",
    "data_new4 = data_new.copy()\n",
    "data_new4['year'] = data_new4.index.year\n",
    "Year_data3 = data_new4.groupby('year')['Log_return'].sum().to_frame()\n",
    "Year_data3['Raw_Return'] = np.exp(Year_data3['Log_return']) - 1\n",
    "\n",
    "# 显示结果\n",
    "print(\"年度对数收益率汇总:\")\n",
    "Year_data1\n",
    "print(\"\\n年末价格计算的收益率:\")\n",
    "Year_data2\n",
    "print(\"\\n使用groupby计算的年度收益率:\")\n",
    "Year_data3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 54,
   "id": "ffb08de9",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "滚动收益率 (基于对数收益率累加):\n",
      "            Rolling_5d_Return  Rolling_10d_Return  Rolling_20d_Return  \\\n",
      "Day                                                                     \n",
      "2025-08-25           0.041720            0.064705            0.079386   \n",
      "2025-08-26           0.037854            0.055229            0.071660   \n",
      "2025-08-27           0.009065            0.031732            0.051064   \n",
      "2025-08-28           0.019225            0.048318            0.075671   \n",
      "2025-08-29           0.008408            0.043594            0.083702   \n",
      "\n",
      "            Rolling_30d_Return  Rolling_60d_Return  \n",
      "Day                                                 \n",
      "2025-08-25            0.103394            0.160143  \n",
      "2025-08-26            0.103676            0.150627  \n",
      "2025-08-27            0.084644            0.125628  \n",
      "2025-08-28            0.092916            0.135781  \n",
      "2025-08-29            0.091511            0.139592  \n",
      "\n",
      "滚动收益率 (基于价格变化):\n",
      "            Rolling_5d_Price_Return  Rolling_10d_Price_Return  \\\n",
      "Day                                                             \n",
      "2025-08-25                 0.041720                  0.064705   \n",
      "2025-08-26                 0.037854                  0.055229   \n",
      "2025-08-27                 0.009065                  0.031732   \n",
      "2025-08-28                 0.019225                  0.048318   \n",
      "2025-08-29                 0.008408                  0.043594   \n",
      "\n",
      "            Rolling_20d_Price_Return  Rolling_30d_Price_Return  \\\n",
      "Day                                                              \n",
      "2025-08-25                  0.079386                  0.103394   \n",
      "2025-08-26                  0.071660                  0.103676   \n",
      "2025-08-27                  0.051064                  0.084644   \n",
      "2025-08-28                  0.075671                  0.092916   \n",
      "2025-08-29                  0.083702                  0.091511   \n",
      "\n",
      "            Rolling_60d_Price_Return  \n",
      "Day                                   \n",
      "2025-08-25                  0.160143  \n",
      "2025-08-26                  0.150627  \n",
      "2025-08-27                  0.125628  \n",
      "2025-08-28                  0.135781  \n",
      "2025-08-29                  0.139592  \n"
     ]
    }
   ],
   "source": [
    "# 计算滚动收益率（例如：过去30天、60天、90天的收益率 注意这里指的是前30个观测值）\n",
    "# 这在金融分析中非常常见，用于观察不同时间窗口的收益表现\n",
    "\n",
    "# 方法1：使用rolling窗口函数计算滚动对数收益率之和\n",
    "rolling_returns = pd.DataFrame()\n",
    "for window in [5, 10, 20, 30, 60]:\n",
    "    # 计算滚动窗口的对数收益率之和\n",
    "    rolling_log_return = data_new['Log_return'].rolling(window=window).sum()\n",
    "    # 转换为原始收益率\n",
    "    rolling_returns[f'Rolling_{window}d_Return'] = np.exp(rolling_log_return) - 1\n",
    "\n",
    "# 方法2：使用pct_change计算滚动价格变化\n",
    "rolling_price_returns = pd.DataFrame()\n",
    "for window in [5, 10, 20, 30, 60]:\n",
    "    rolling_price_returns[f'Rolling_{window}d_Price_Return'] = data_new['Close'].pct_change(periods=window)\n",
    "\n",
    "# 显示结果\n",
    "print(\"滚动收益率 (基于对数收益率累加):\")\n",
    "print(rolling_returns.tail())\n",
    "print(\"\\n滚动收益率 (基于价格变化):\")\n",
    "print(rolling_price_returns.tail())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 55,
   "id": "a121c2b1",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "不同方法计算的累积收益率:\n"
     ]
    },
    {
     "data": {
      "application/vnd.microsoft.datawrangler.viewer.v0+json": {
       "columns": [
        {
         "name": "Day",
         "rawType": "datetime64[ns]",
         "type": "datetime"
        },
        {
         "name": "Cumulative_Log_Return",
         "rawType": "float64",
         "type": "float"
        },
        {
         "name": "Cumulative_Return",
         "rawType": "float64",
         "type": "float"
        },
        {
         "name": "Cumulative_Return_Prod",
         "rawType": "float64",
         "type": "float"
        },
        {
         "name": "Cumulative_Return_Alt",
         "rawType": "float64",
         "type": "float"
        }
       ],
       "ref": "07f2603b-f54a-4e40-afee-fed4b48fba99",
       "rows": [
        [
         "1995-01-03 00:00:00",
         "-0.012409400168589713",
         "-0.012332721070584918",
         "-0.01233272107058514",
         "-0.01233272107058514"
        ],
        [
         "1995-01-04 00:00:00",
         "0.009126730428462793",
         "0.009168506027443568",
         "0.00916850602744379",
         "0.00916850602744379"
        ],
        [
         "1995-01-05 00:00:00",
         "-0.0015137943513012786",
         "-0.0015126491425753175",
         "-0.0015126491425748734",
         "-0.0015126491425748734"
        ],
        [
         "1995-01-06 00:00:00",
         "-0.011035087108702157",
         "-0.010974423881334183",
         "-0.010974423881334183",
         "-0.010974423881334183"
        ],
        [
         "1995-01-09 00:00:00",
         "-0.03433968784188668",
         "-0.03375677219195239",
         "-0.033756772191952056",
         "-0.033756772191952056"
        ],
        [
         "1995-01-10 00:00:00",
         "-0.05973941939153615",
         "-0.05799002886381577",
         "-0.057990028863815324",
         "-0.057990028863815324"
        ],
        [
         "1995-01-11 00:00:00",
         "-0.05511307371167451",
         "-0.05362186858474738",
         "-0.053621868584746823",
         "-0.053621868584746823"
        ],
        [
         "1995-01-12 00:00:00",
         "-0.07323329794968192",
         "-0.0706160186457161",
         "-0.07061601864571587",
         "-0.07061601864571587"
        ],
        [
         "1995-01-13 00:00:00",
         "-0.08036689931995067",
         "-0.07722228224798222",
         "-0.07722228224798178",
         "-0.07722228224798178"
        ],
        [
         "1995-01-16 00:00:00",
         "-0.09558650797588797",
         "-0.09116026363313656",
         "-0.09116026363313634",
         "-0.09116026363313634"
        ],
        [
         "1995-01-17 00:00:00",
         "-0.09706515706620245",
         "-0.09250312562705443",
         "-0.09250312562705465",
         "-0.09250312562705465"
        ],
        [
         "1995-01-18 00:00:00",
         "-0.08741698391436792",
         "-0.08370506428758906",
         "-0.0837050642875885",
         "-0.0837050642875885"
        ],
        [
         "1995-01-19 00:00:00",
         "-0.12504069790107053",
         "-0.11753901245620269",
         "-0.11753901245620257",
         "-0.11753901245620257"
        ],
        [
         "1995-01-20 00:00:00",
         "-0.1258806223572524",
         "-0.11827990183215753",
         "-0.11827990183215764",
         "-0.11827990183215764"
        ],
        [
         "1995-01-23 00:00:00",
         "-0.15753671375312628",
         "-0.14575454952382416",
         "-0.14575454952382405",
         "-0.14575454952382405"
        ],
        [
         "1995-01-24 00:00:00",
         "-0.16819975154266142",
         "-0.1548150091839412",
         "-0.15481500918394098",
         "-0.15481500918394098"
        ],
        [
         "1995-01-25 00:00:00",
         "-0.1533355193408017",
         "-0.1421581490113758",
         "-0.1421581490113757",
         "-0.1421581490113757"
        ],
        [
         "1995-01-26 00:00:00",
         "-0.15813316219157603",
         "-0.14626391096979352",
         "-0.1462639109697932",
         "-0.1462639109697932"
        ],
        [
         "1995-01-27 00:00:00",
         "-0.14113893766204466",
         "-0.13163134579468116",
         "-0.13163134579468083",
         "-0.13163134579468083"
        ],
        [
         "1995-02-06 00:00:00",
         "-0.19381870206776508",
         "-0.17619275471931106",
         "-0.17619275471931073",
         "-0.17619275471931073"
        ],
        [
         "1995-02-07 00:00:00",
         "-0.1961259408774616",
         "-0.17809128374519567",
         "-0.17809128374519556",
         "-0.17809128374519556"
        ],
        [
         "1995-02-08 00:00:00",
         "-0.1937624943991505",
         "-0.17614644913331368",
         "-0.17614644913331357",
         "-0.17614644913331357"
        ],
        [
         "1995-02-09 00:00:00",
         "-0.1576812746496561",
         "-0.14587803108648378",
         "-0.14587803108648312",
         "-0.14587803108648312"
        ],
        [
         "1995-02-10 00:00:00",
         "-0.1795686237075209",
         "-0.16436939509469517",
         "-0.16436939509469473",
         "-0.16436939509469473"
        ],
        [
         "1995-02-13 00:00:00",
         "-0.16588309888901076",
         "-0.152854739376727",
         "-0.15285473937672667",
         "-0.15285473937672667"
        ],
        [
         "1995-02-14 00:00:00",
         "-0.16721406221051804",
         "-0.15398150863599158",
         "-0.1539815086359917",
         "-0.1539815086359917"
        ],
        [
         "1995-02-15 00:00:00",
         "-0.1768754417288152",
         "-0.16211585657616512",
         "-0.16211585657616479",
         "-0.16211585657616479"
        ],
        [
         "1995-02-16 00:00:00",
         "-0.18514400281770893",
         "-0.1690153888897462",
         "-0.16901538888974632",
         "-0.16901538888974632"
        ],
        [
         "1995-02-17 00:00:00",
         "-0.19374375921165754",
         "-0.17613101393798125",
         "-0.17613101393798136",
         "-0.17613101393798136"
        ],
        [
         "1995-02-20 00:00:00",
         "-0.19149808119626144",
         "-0.17427879049809347",
         "-0.1742787904980937",
         "-0.1742787904980937"
        ],
        [
         "1995-02-21 00:00:00",
         "-0.1800120335363129",
         "-0.16473983978267226",
         "-0.16473983978267248",
         "-0.16473983978267248"
        ],
        [
         "1995-02-22 00:00:00",
         "-0.0857338785249473",
         "-0.08216154475434922",
         "-0.08216154475434889",
         "-0.08216154475434889"
        ],
        [
         "1995-02-23 00:00:00",
         "-0.09504318657094046",
         "-0.09066633738249996",
         "-0.09066633738249952",
         "-0.09066633738249952"
        ],
        [
         "1995-02-24 00:00:00",
         "-0.14856078400858763",
         "-0.13805238705295808",
         "-0.1380523870529582",
         "-0.1380523870529582"
        ],
        [
         "1995-02-27 00:00:00",
         "-0.14348798577654787",
         "-0.13366879157855727",
         "-0.13366879157855738",
         "-0.13366879157855738"
        ],
        [
         "1995-02-28 00:00:00",
         "-0.16511814119757862",
         "-0.15220646117276604",
         "-0.15220646117276615",
         "-0.15220646117276615"
        ],
        [
         "1995-03-01 00:00:00",
         "-0.15119663487885582",
         "-0.14032136076682022",
         "-0.14032136076682045",
         "-0.14032136076682045"
        ],
        [
         "1995-03-02 00:00:00",
         "-0.14423656885075253",
         "-0.13431706978251812",
         "-0.134317069782518",
         "-0.134317069782518"
        ],
        [
         "1995-03-03 00:00:00",
         "-0.11546658260791798",
         "-0.1090496550233846",
         "-0.10904965502338415",
         "-0.10904965502338415"
        ],
        [
         "1995-03-06 00:00:00",
         "-0.04995620097378772",
         "-0.04872891166437732",
         "-0.048728911664376984",
         "-0.048728911664376984"
        ],
        [
         "1995-03-07 00:00:00",
         "-0.02328282994769193",
         "-0.02301387624060347",
         "-0.02301387624060358",
         "-0.02301387624060358"
        ],
        [
         "1995-03-08 00:00:00",
         "-0.04266498195528978",
         "-0.04176763856946619",
         "-0.04176763856946597",
         "-0.04176763856946597"
        ],
        [
         "1995-03-09 00:00:00",
         "-0.06748686950461735",
         "-0.0652600058653744",
         "-0.06526000586537395",
         "-0.06526000586537395"
        ],
        [
         "1995-03-10 00:00:00",
         "-0.09987551123188965",
         "-0.09504993285690022",
         "-0.09504993285690011",
         "-0.09504993285690011"
        ],
        [
         "1995-03-13 00:00:00",
         "-0.09997785490202293",
         "-0.09514254402889455",
         "-0.09514254402889444",
         "-0.09514254402889444"
        ],
        [
         "1995-03-14 00:00:00",
         "-0.09655503105401841",
         "-0.09204006976708323",
         "-0.09204006976708268",
         "-0.09204006976708268"
        ],
        [
         "1995-03-15 00:00:00",
         "-0.12350266627796991",
         "-0.1161807152669514",
         "-0.1161807152669515",
         "-0.1161807152669515"
        ],
        [
         "1995-03-16 00:00:00",
         "-0.11086887335928797",
         "-0.10494389306496654",
         "-0.10494389306496665",
         "-0.10494389306496665"
        ],
        [
         "1995-03-17 00:00:00",
         "-0.08761914706071483",
         "-0.0838902866315776",
         "-0.08389028663157738",
         "-0.08389028663157738"
        ],
        [
         "1995-03-20 00:00:00",
         "-0.10130927664725142",
         "-0.09634648926482181",
         "-0.09634648926482159",
         "-0.09634648926482159"
        ]
       ],
       "shape": {
        "columns": 4,
        "rows": 7445
       }
      },
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Cumulative_Log_Return</th>\n",
       "      <th>Cumulative_Return</th>\n",
       "      <th>Cumulative_Return_Prod</th>\n",
       "      <th>Cumulative_Return_Alt</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Day</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>1995-01-03</th>\n",
       "      <td>-0.012409</td>\n",
       "      <td>-0.012333</td>\n",
       "      <td>-0.012333</td>\n",
       "      <td>-0.012333</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1995-01-04</th>\n",
       "      <td>0.009127</td>\n",
       "      <td>0.009169</td>\n",
       "      <td>0.009169</td>\n",
       "      <td>0.009169</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1995-01-05</th>\n",
       "      <td>-0.001514</td>\n",
       "      <td>-0.001513</td>\n",
       "      <td>-0.001513</td>\n",
       "      <td>-0.001513</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1995-01-06</th>\n",
       "      <td>-0.011035</td>\n",
       "      <td>-0.010974</td>\n",
       "      <td>-0.010974</td>\n",
       "      <td>-0.010974</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1995-01-09</th>\n",
       "      <td>-0.034340</td>\n",
       "      <td>-0.033757</td>\n",
       "      <td>-0.033757</td>\n",
       "      <td>-0.033757</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2025-08-25</th>\n",
       "      <td>1.790819</td>\n",
       "      <td>4.994361</td>\n",
       "      <td>4.994361</td>\n",
       "      <td>4.994361</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2025-08-26</th>\n",
       "      <td>1.786903</td>\n",
       "      <td>4.970931</td>\n",
       "      <td>4.970931</td>\n",
       "      <td>4.970931</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2025-08-27</th>\n",
       "      <td>1.769160</td>\n",
       "      <td>4.865922</td>\n",
       "      <td>4.865922</td>\n",
       "      <td>4.865922</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2025-08-28</th>\n",
       "      <td>1.780475</td>\n",
       "      <td>4.932675</td>\n",
       "      <td>4.932675</td>\n",
       "      <td>4.932675</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2025-08-29</th>\n",
       "      <td>1.784196</td>\n",
       "      <td>4.954793</td>\n",
       "      <td>4.954793</td>\n",
       "      <td>4.954793</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>7445 rows × 4 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "            Cumulative_Log_Return  Cumulative_Return  Cumulative_Return_Prod  \\\n",
       "Day                                                                            \n",
       "1995-01-03              -0.012409          -0.012333               -0.012333   \n",
       "1995-01-04               0.009127           0.009169                0.009169   \n",
       "1995-01-05              -0.001514          -0.001513               -0.001513   \n",
       "1995-01-06              -0.011035          -0.010974               -0.010974   \n",
       "1995-01-09              -0.034340          -0.033757               -0.033757   \n",
       "...                           ...                ...                     ...   \n",
       "2025-08-25               1.790819           4.994361                4.994361   \n",
       "2025-08-26               1.786903           4.970931                4.970931   \n",
       "2025-08-27               1.769160           4.865922                4.865922   \n",
       "2025-08-28               1.780475           4.932675                4.932675   \n",
       "2025-08-29               1.784196           4.954793                4.954793   \n",
       "\n",
       "            Cumulative_Return_Alt  \n",
       "Day                                \n",
       "1995-01-03              -0.012333  \n",
       "1995-01-04               0.009169  \n",
       "1995-01-05              -0.001513  \n",
       "1995-01-06              -0.010974  \n",
       "1995-01-09              -0.033757  \n",
       "...                           ...  \n",
       "2025-08-25               4.994361  \n",
       "2025-08-26               4.970931  \n",
       "2025-08-27               4.865922  \n",
       "2025-08-28               4.932675  \n",
       "2025-08-29               4.954793  \n",
       "\n",
       "[7445 rows x 4 columns]"
      ]
     },
     "execution_count": 55,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 计算累积收益率\n",
    "# 累积收益率用于观察长期投资表现，从某个起始点开始累积\n",
    "\n",
    "# 方法1：使用对数收益率累加后转换\n",
    "# 这是最准确的方法，特别是对于长期累积\n",
    "cumulative_returns = pd.DataFrame()\n",
    "cumulative_returns['Cumulative_Log_Return'] = data_new['Log_return'].cumsum()\n",
    "cumulative_returns['Cumulative_Return'] = np.exp(cumulative_returns['Cumulative_Log_Return']) - 1\n",
    "\n",
    "# 方法2：使用cumprod函数直接累乘(1+r)\n",
    "# 这种方法在金融实践中也很常见\n",
    "cumulative_returns['Cumulative_Return_Prod'] = (1 + data_new['Raw_return']).cumprod() - 1\n",
    "\n",
    "# 方法3：使用pandas的累积函数\n",
    "cumulative_returns['Cumulative_Return_Alt'] = data_new['Raw_return'].add(1).cumprod().sub(1)\n",
    "\n",
    "# 显示结果\n",
    "print(\"不同方法计算的累积收益率:\")\n",
    "cumulative_returns"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "py3.13",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.13.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
